Laravel 5 User Model not found

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

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

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

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

I have the correct entry in my json file:

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

What have I missed?

回答1:

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



回答2:

Don't forget to clear cache after all previous changes

php artisan config:clear 


回答3:

Don't forget to change your User.php namespace.

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

<?php namespace App; 

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

<?php namespace App\Models; 

Hope that helps.



回答4:

* 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.

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

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.

<?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:

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

4) I modified auth.php as follows:

/* |-------------------------------------------------------------------------- | 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:

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

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

composer dump-autoload  php artisan migrate --seed 

Hope this helps!



回答5:

Don't forget to change

use App\User; 

to

use App\Models\User; 

in the AuthController.php



回答6:

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

composer dump-autoload  

to refresh :)



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!