Laravel 5.3,找不到类“App\Models\User”

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

Laravel 5.3, Class 'App\Models\User' not found

laravel

提问by changer

I have laravel app.

我有 Laravel 应用程序。

It works locally, using php artisan servecommand.

它使用php artisan serve命令在本地工作。

After deployment to remote server it does not work.

部署到远程服务器后,它不起作用。

Every artisan command returns:

每个工匠命令都会返回:

 [Symfony\Component\Debug\Exception\FatalThrowableError]  
 Class 'App\Models\User' not found      

Excerpt from App\Models\User.php(case-sensitive).

摘自App\Models\User.php(区分大小写)。

<?
namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {
// code here
}

composer dump-autoload does not fix it.

composer dump-autoload 没有修复它。

.gitignore

.gitignore

/vendor
.env
/public/css/
/public/js/
/public/img/
/public/fonts/
/node_modules/

composer.json

作曲家.json

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.3.*",
    "caouecs/laravel-lang": "~3.0"
},
"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~5.0",
    "symfony/css-selector": "3.1.*",
    "symfony/dom-crawler": "3.1.*"
},
"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/",
        "Seeds\": "database/seeds/"
    },
    "files": [
        "app/Http/Helpers.php"
    ]
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},
"scripts": {
    "post-root-package-install": [
        "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\Foundation\ComposerScripts::postInstall",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "Illuminate\Foundation\ComposerScripts::postUpdate",
        "php artisan optimize"
    ]
},
"config": {
    "preferred-install": "dist"
}

}

}

Local system: arch linux with php 7.0 Remote system: debian jessie with php 7.0

本地系统:arch linux with php 7.0 远程系统:debian jessie with php 7.0

回答by Alexey Mezenin

Add useclause at the top of a controller where you're trying to use the model:

use在您尝试使用模型的控制器顶部添加子句:

use App\Models\User;

Alternatively, you can use full namespace:

或者,您可以使用完整的命名空间:

$user = App\Models\User::find(1);

Also, make sure User.phpis in the app\Modelsdirectory and make sure you've changed model in config\auth.phpfile:

另外,请确保User.phpapp\Models目录中并确保您已更改config\auth.php文件中的模型:

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

回答by changer

Finally, I found a solution.

最后,我找到了解决方案。

In php.ini set short_open_tag = Onand voila - it works.

在 php.ini 设置中short_open_tag = On,瞧 - 它有效。

回答by Gpak

Check the generated cached files, might still be referencing the old namespace.

检查生成的缓存文件,可能仍在引用旧命名空间。

rm -r bootrap/cacheshould to the trick

rm -r bootrap/cache应该出招

回答by Black Mamba

I was using use App\Models\User;in my controller when I copied my models from root of app folder to Models folder inside app folder.

use App\Models\User;当我将模型从 app 文件夹的根目录复制到 app 文件夹内的 Models 文件夹时,我在控制器中使用。

An additional change was required which was using namespace App\Models;Inside my models so now my User model looks like:

需要进行额外更改,该更改使用namespace App\Models;我的模型内部,因此现在我的用户模型如下所示:

<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends Model implements AuthenticatableContract, AuthorizableContract