php 未找到 Laravel 5 用户模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28516454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:54:45  来源:igfitidea点击:

Laravel 5 User Model not found

phplaravellaravel-5

提问by imperium2335

I have moved the User model from the default app directory into app/Models.

我已将 User 模型从默认应用程序目录移动到 app/Models 中。

I have updated the namespace in User to namespace App\Models;but I am getting this error:

我已将 User 中的命名空间更新为,namespace App\Models;但出现此错误:

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

I have the correct entry in my json file:

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

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

What have I missed?

我错过了什么?

回答by Martin Bean

You need to update your config/auth.phpfile. Change 'model' => 'App\User'to 'model' => 'App\Models\User'.

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

回答by AURELIEN LE GRAND

Don't forget to clear cache after all previous changes

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

php artisan config:clear

回答by Joe Tannorella

Don't forget to change your User.phpnamespace.

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

E.g. If your User model is located at /App/User.phpthen the first line should read:

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

<?php namespace App;

However, if you've created a /Modelsdirectory and your User model is now located at /App/Models/User.php, the first line of User.php should reference this new namespace:

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

<?php namespace App\Models;

Hope that helps.

希望有帮助。

回答by clone45

* Note: My tactic is probably technically 'incorrect' as it pollutes the global namespace. However, I had trouble getting the other answers working and post this only as a last resort. *

* 注意:我的策略在技术上可能是“不正确的”,因为它会污染全局命名空间。但是,我无法获得其他答案,因此仅将其作为最后的手段发布。*

I did this a slightly different way than the other answers. I'm also using Laravel 5.

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

1) I created the app/Models directory and moved User.php into that directory.

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

2) I modified /app/Models/User.php by removing the namespace at the top. This might be polluting the global namespace, but it's the only way I could get it working.

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) I added "app/Models" to my autoload section in composer.json:

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

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

4) I modified auth.php as follows:

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',

For reference, here's my users table migration:

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

<?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');
    }
}

And here's my UsersTableSeeder.php file:

这是我的 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 protected]', 'password' => Hash::make('my raw dev password')]
        ];

        foreach($users as $user){
            \User::create($user);
        }
    }
}

?>

After all of those updates were done, on the command line I ran:

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

composer dump-autoload 
php artisan migrate --seed

Hope this helps!

希望这可以帮助!

回答by Eqwo

Don't forget to change

不要忘记改变

use App\User;

to

use App\Models\User;

in the AuthController.php

在 AuthController.php 中

回答by Thomas Bolander

If you have your models in a specific folder, then you need to run

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

composer dump-autoload 

to refresh :)

刷新:)