Laravel 8 - Illuminate\Auth\EloquentUserProvider::validateCredentials()

Laravel 8 - Illuminate\Auth\EloquentUserProvider::validateCredentials()

我有一个应用程序,我想在其中进行身份验证但不使用用户模型。该模型称为 Karyawan。但是当我更改 auth.php 它开始显示此错误:

Illuminate\Auth\EloquentUserProvider::validateCredentials(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Models\Karyawan given, called in C:\KKP\Project_KKP\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 426

Laravel版本为8

auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

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

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
        'throttle' => 60,
    ],
],

App\Models\Karyawan.php

namespace App\Models;

use App\Models\Cuti;
use App\Models\Role;
use App\Models\Divisi;
use App\Models\Jabatan;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Karyawan extends Model
{
    use HasFactory;

    protected $guarded = ['karyawan_id'];
    protected $table = 'karyawans';

    public function scopeSearch($query, array $searchs) {

        $query->when($searchs['search'] ?? false, function($query, $search) {
            return $query->where('nama', 'like', '%' . $search . '%')
            ->orWhere('divisi', 'like', '%' . $search . '%') 
            ->orWhere('jabatan', 'like', '%' . $search . '%')
            ->orWhere('agama', 'like', '%' . $search . '%')
            ->orWhere('nik', 'like', '%' . $search . '%'); 
        });
    }

    public function role_id()
    {
        return $this->belongsTo(Role::class);
    }

    public function divisi()
    {
        return $this->belongsTo(Divisi::class);
    }

    public function jabatan()
    {
        return $this->belongsTo(Jabatan::class);
    }

    public function cuti()
    {
        return $this->hasMany(Cuti::class);
    }
}

web.php

Route::get('/', [BasicLoginController::class, 'login']);
Route::post('/', [BasicLoginController::class, 'authenticate']);

BasicLoginController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BasicLoginController extends Controller
{

public function login()
{
    return view ('login.login', [
        'title' => 'Login'
    ]);
}

public function authenticate(Request $request) {
    $credentials = $request->validate([
        'email' => 'required|email:dns',
        'password' => 'required'
    ]);

    if(Auth::attempt($credentials)) {
        $request->session()->regenerate();
        return redirect()->intended('/dashboard');
    }

    return back()->with('loginError', 'Email atau Password salah');
}
}

如错误所述,您的 class 需要实现接口 Illuminate\Contracts\Auth\Authenticatable,普通用户模型通过扩展 class Illuminate\Foundation\Auth\User as Authenticatable;

因此,要么在您的 class 中实施契约,并在其中声明所有需要的方法功能。

只需扩展现有的 Illuminate\Foundation\Auth\User

namespace App\Models;

//...
use Illuminate\Foundation\Auth\User as Authenticatable;

class Karyawan extends Authenticatable
{

class Illuminate\Foundation\Auth\User 扩展模型 class Illuminate\Database\Eloquent\Model 因此模型功能仍然可用。

编辑:

在这里,复制粘贴这个模型,你应该学习一些PSRs especially PSR-4

namespace App\Models;

use App\Models\Cuti;
use App\Models\Role;
use App\Models\Divisi;
use App\Models\Jabatan;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Karyawan extends Authenticatable
{
    use HasFactory;

    protected $guarded = ['karyawan_id'];
    protected $table = 'karyawans';

    public function scopeSearch($query, array $searchs) {

        $query->when($searchs['search'] ?? false, function($query, $search) {
            return $query->where('nama', 'like', '%' . $search . '%')
            ->orWhere('divisi', 'like', '%' . $search . '%') 
            ->orWhere('jabatan', 'like', '%' . $search . '%')
            ->orWhere('agama', 'like', '%' . $search . '%')
            ->orWhere('nik', 'like', '%' . $search . '%'); 
        });
    }

    public function role_id()
    {
        return $this->belongsTo(Role::class);
    }

    public function divisi()
    {
        return $this->belongsTo(Divisi::class);
    }

    public function jabatan()
    {
        return $this->belongsTo(Jabatan::class);
    }

    public function cuti()
    {
        return $this->hasMany(Cuti::class);
    }
}

Class Karyawan 需要实现 Illuminate\Contracts\Auth\Authenticatableclass 接口。 或者您可以在 Karyawan Class.

中扩展 Illuminate\Foundation\Auth\User