Home > Mobile >  How to change columns used by default by laravel sanctum (email and password) by custom ones?
How to change columns used by default by laravel sanctum (email and password) by custom ones?

Time:11-10

I just installed laravel, sanctrum, livewire as new auth system. I need to use an already created table for users. I am trying to change my User model for custom one. Thing is that this other model have no defaults columns (email and password) so I need to personalize how sanctrum is using this. I already create de Model and changed in config/auth.php the default model:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Usuario::class,
        ],

Then I am getting error :

Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from `USUARIOS` where `email` = [email protected] limit 1)

I know is not too much info, but probably someone had before to do this. Thanks in advance.

CodePudding user response:

The answer was in files of the folder App\Actions\Fortify. All this files have the logic functions that use fields for login and password traitments.

Per example:

App\Actions\Fortify\CreateNewUser.php file make the validation for the register form and create a new User. So, changing there your custom names for fields will make to work the registration form in sanctrum.

public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
        ])->validate();

        return Usuario::create([
            'custom_name_for_name' => $input['name'],
            'custom_name_for_email' => $input['email'],
            'custom_name_for_password' => Hash::make($input['password']),
        ]);
    }

So, in resume:

1- We need to create the Custom User Model.

2- Change in config/auth.php the default model.

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Usuario::class,
        ],

3- Then modify fields names in files of App\Actions\Fortify

  • Related