到用户只能编辑自己的资料时出错,报 "Class '\App\Models\User' not found"。我命名空间和文件路径都是对的,这是为什么呢?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5

你可能是没有引入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;
5年前 评论

这是什么错误呢 Call to undefined method Illuminate\Database\Query\Builder::getAuthIdentifierName() ?

5年前 评论

@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() 这个错误

5年前 评论

@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

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!