Class '\App\User' not found in Laravel when changing the namespace

会有一股神秘感。 提交于 2019-11-29 00:39:06

问题


I am having this error when moving User.php to Models/User.php

local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class '\App\User' not found

vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php:126


回答1:


Go to config/auth.php and change App\User:class to App\Models\User::class.

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

Also change the namespace of User.php model

namespace App\Models;



回答2:


These answers aren't right, you don't need to change the namespace to \App\Models\User. The autoload will load the models folder but the class can still be class User and the namespace should still be App. Is that how it is set up in your file?

namespace App;

class User extends Model {}



回答3:


I finally was able to resolve it by changing this the following code.

 array (
        'driver' => 'eloquent',
        'model' => 'App\\Models\User',
      ),



回答4:


What solved it for me was to change:

            'model' => '{YourAppName}\User',



回答5:


If you are use the auth default on Laravel (php artisan make:auth), you have change the RegisterController on app/Http/Controllers/Auth/

use App\User;

to

use App\Models\User;

Also, for the rest of functionality, you have change the namespace on you User Model:

namespace App\Models;

And change config/auth.php

'providers' => [
'users' => [
    'driver' => 'eloquent',
    'model' => App\Models\User::class,
],



回答6:


Reload composer autloaded classes.

composer dump-autoload



回答7:


What happened is that you changed the location of the file user.php.

Your system is still looking for the file user.php in the old location. You need to give the system the right road to the file.

I gess you have to change the the code from 'model' => App\User::class, to

'model' => App\Models\User::class,




回答8:


if you are using user.php model file into folder Models/user.php then you need to change in follwing file so You will not get any Error

where to change if we create Model Folder in App\http ??

changer in folowing path ---

1 Config - - auth.php - search for users key change ---> app\user TO app\Models\user

2 venedor/composer/ -autoload_classmap.php ----> BAse path (app\user TO app\Models\user) -autoload_static.php ----> BAse path (app\user TO app\Models\user)




回答9:


You need to change App\User to App\Models\User in config/auth.php




回答10:


If the app config is cached, it may not be able to get the new configuration, because config:cache gives this error before clearing and caching configuration, so just delete the cache manually:

rm bootstrap/cache/config.php



回答11:


Check if your importations represent the exact name of your class. I found in one of my controllers I was imported App\user with "u" on lowercase instead of App\User with 'u' in uppercase




回答12:


I fixed the problem changing the use App\User; to use MyAppName\User;



来源:https://stackoverflow.com/questions/35821477/class-app-user-not-found-in-laravel-when-changing-the-namespace

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