你可能是没有引入User模型的命名空间。app/Policies/UserPolicy.php文件中,添加use App\Models\User;:
<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use App\Models\User; //引入User的命名空间
.
.
.
或者检查一下User模型的命名空间是否正确,应该是:
namespace App\Models;
@tsin UserPolicy.php文件我正常使用了 use App\Models\User; user
模型的命名空间也是对的 好像是 config/auth.php 的
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
]
这个地方改成 App\Models\User::class,然后就报 Call to undefined method Illuminate\Database\Query\Builder::getAuthIdentifierName() 这个错误
@zhaozhenghong getAuthIdentifierName() 这个函数位于 /vendor/laravel/framework/src/Illuminate/Auth/Authenticatable.php, 在 /vendor/laravel/framework/src/Illuminate/Foundation/Auth/User.php 中引入, User模型中使用到该 User.php :
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; //-->这里引入
use App\Notifications\ResetPassword;
use Auth;
//-->这里继承Illuminate\Foundation\Auth\User
class User extends Authenticatable
{
.
.
.
所以需要检查一下User模型文件是否包含以上代码。
另外还要检查User表的主键是否是id。
关于 LearnKu
推荐文章: