Skip to content
Advertisement

Laravel how to make the Auth class execute functions on different model rather the User

I am trying to make login and logout function in model/controller named Admin and there I already have User model/controller but it’s not for the Auth purpose, when I am trying to call Auth::attempt() or Auth::logout() it returns this error

Call to undefined method AppModelsUser::getAuthIdentifierName()

here is my code

the Admin model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;   

class Admin extends Authenticatable
{
    use HasFactory;

    protected $fillable = ['username', 'password'];

    protected $hidden = ['password'];
}

the User model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class User extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'national_id', 'check_lab', 'check', 'visit_date_to', 
    'user_password', 'report_time', 'reference_id', 'passport_id', 'result', 
    'visit_date_from', 
    'embassy', 'report_date'];
}

Advertisement

Answer

If you want to use Admin Modal instead of the User model for auth then you need to change the default Modal in config/auth.php

From

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsUser::class,
        ],

To

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsAdmin::class,
        ],
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement