Laravel 5 User Model not found

2019-03-11 00:11发布

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?

6条回答
姐就是有狂的资本
2楼-- · 2019-03-11 00:47

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

查看更多
闹够了就滚
3楼-- · 2019-03-11 00:47

Don't forget to change

use App\User;

to

use App\Models\User;

in the AuthController.php

查看更多
4楼-- · 2019-03-11 00:48

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-03-11 01:01

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

查看更多
爷的心禁止访问
6楼-- · 2019-03-11 01:10

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

composer dump-autoload 

to refresh :)

查看更多
看我几分像从前
7楼-- · 2019-03-11 01:12

Don't forget to clear cache after all previous changes

php artisan config:clear
查看更多
登录 后发表回答