未找到Laravel 5用户模型


Laravel 5 User Model not found

我已经将用户模型从默认的应用程序目录移到了app/Models中。

我已经将User中的命名空间更新为namespace App'Models;,但我收到了以下错误:

FatalErrorException in EloquentUserProvider.php line 122:
Class ''App'User' not found

我在json文件中有正确的条目:

"autoload": {
    "classmap": [
        "database",
        "app/Modules",
        "app/Models"
    ],
    "psr-4": {
        "App''": "app/",
        "Modules''": "app/Modules/"
    }
},

我错过了什么?

您需要更新config/auth.php文件。将'model' => 'App'User'更改为'model' => 'App'Models'User'

不要忘记在之前的所有更改后清除缓存

php artisan config:clear

不要忘记更改User.php命名空间。

例如,如果您的用户型号位于/App/User.php,则第一行应为:

<?php namespace App;

但是,如果您已经创建了一个/Models目录,并且您的User模型现在位于/App/Models/User.php,那么User.php的第一行应该引用这个新名称空间:

<?php namespace App'Models;

希望能有所帮助。

*注意:我的策略在技术上可能是"不正确的",因为它污染了全局命名空间。然而,我很难得到其他答案,只能在万不得已的情况下发布*

我这样做的方式与其他答案略有不同。我也在使用Laravel 5。

1) 我创建了app/Models目录,并将User.php移到该目录中。

2) 我通过删除顶部的命名空间修改了/app/Models/User.php。这可能会污染全局命名空间,但这是我让它工作的唯一方法。

<?php  // !!!! NOTICE no namespace here  !!!!!
use Illuminate'Auth'Authenticatable;
use Illuminate'Database'Eloquent'Model;
use Illuminate'Auth'Passwords'CanResetPassword;
use Illuminate'Contracts'Auth'Authenticatable as AuthenticatableContract;
use Illuminate'Contracts'Auth'CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
    use Authenticatable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['first_name', 'last_name', 'email', 'password'];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];
}

3) 我在composer.json:中的自动加载部分添加了"app/Models"

"autoload": {
    "classmap": [
        "database",
        "app/Models"    // <==== I added this               
    ],
    "psr-4": {
        "App''": "app/"
    }
},

4) 我修改auth.php如下:

/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => ''User',

作为参考,以下是我的用户表迁移:

<?php
use Illuminate'Database'Schema'Blueprint;
use Illuminate'Database'Migrations'Migration;
class CreateUsersTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

这是我的UsersTableSeeder.php文件:

<?PHP
use Illuminate'Database'Seeder;
use Illuminate'Database'Eloquent'Model;
class UsersTableSeeder extends Seeder {
    public function run()
    {
        DB::table('users')->delete();
        $users = [
            [ 'id' => 1, 'first_name' => 'Bret', 'last_name' => 'Lastname', 'email' => 'email@domain.com', 'password' => Hash::make('my raw dev password')]
        ];
        foreach($users as $user){
            'User::create($user);
        }
    }
}
?>

所有这些更新完成后,我在命令行上运行:

composer dump-autoload 
php artisan migrate --seed

希望这能有所帮助!

别忘了更改

use App'User;

use App'Models'User;

在AuthController.php 中

如果您的模型位于特定文件夹中,则需要运行

composer dump-autoload 

刷新:)