更改命名空间时在 Laravel 中找不到类“\App\User”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35821477/
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-09-08 17:17:44  来源:igfitidea点击:

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

laravellaravel-5.2

提问by manshu

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

移动User.php到时出现此错误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

local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: 致命错误: Class '\App\User' not found

供应商/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php:126

回答by Med

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

转到 config/auth.php 并将 App\User:class 更改为 App\Models\User::class。

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

Also change the namespace of User.php model

还要更改 User.php 模型的命名空间

namespace App\Models;

回答by Jeff

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 Userand the namespace should still be App. Is that how it is set up in your file?

这些答案是不正确的,您不需要将命名空间更改为 \App\Models\User。自动加载将加载模型文件夹,但类仍然可以是class User,命名空间应该仍然是App. 你的文件里是这样设置的吗?

namespace App;

class User extends Model {}

回答by Pablo Javier Vera Henríquez

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

如果您在使用上Laravel(在auth默认php artisan make:auth),你必须改变RegisterControllerapp/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

并更改 config/auth.php

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

回答by Benjamin Beganovi?

Reload composer autloaded classes.

重新加载作曲家自动加载的类。

composer dump-autoload

回答by Antonio Carlos Ribeiro

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:

如果app config被缓存了,可能无法获取到新的配置,因为config:cache在清除缓存配置之前就报了这个错误,所以手动删除缓存即可:

rm bootstrap/cache/config.php

回答by PraKash

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

如果您在文件夹 Models/user.php 中使用 user.php 模型文件,那么您需要更改以下文件,这样您就不会收到任何错误

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

如果我们在 App\http 中创建模型文件夹,在哪里更改?

changer in folowing path ---

以下路径中的转换器---

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

1 配置 - - auth.php - 搜索用户密钥更改 ---> 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)

2 venedor/composer/ -autoload_classmap.php ----> Base 路径(app\user TO app\Models\user)-autoload_static.php ----> Base 路径(app\user TO app\Models\user)

回答by manshu

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

我终于能够通过更改以下代码来解决它。

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

回答by Marco Santana

What solved it for me was to change:

为我解决的是改变:

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

回答by Alexei

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

发生的情况是您更改了文件 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.

您的系统仍在旧位置寻找文件 user.php。您需要为系统提供文件的正确路径。

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

我猜你必须将代码从“模型”=> App\User::class 更改为

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

'模型' => App\Models\User::class,

回答by rai2

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

检查您的输入是否代表您班级的确切名称。我发现在我的一个控制器中,我是App\user用小写的“u”而不是App\User大写的“u”导入的