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

Time:10-05

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 App\Models\User::getAuthIdentifierName()

here is my code

the Admin model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;   

class Admin extends Authenticatable
{
    use HasFactory;

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

    protected $hidden = ['password'];
}

the User model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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'];
}

CodePudding user response:

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' => App\Models\User::class,
        ],

To

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