【问题标题】:Laravel 5 User Model not found未找到 Laravel 5 用户模型
【发布时间】:2015-04-15 11:45:23
【问题描述】:

我已将用户模型从默认应用目录移动到 app/Models。

我已将 User 中的命名空间更新为 namespace App\Models;,但出现此错误:

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

我的 json 文件中有正确的条目:

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

我错过了什么?

【问题讨论】:

  • 你试过composer dump-autoload吗?
  • @fakemeta 是的,仍然给出错误:(
  • 您是尝试使用 laravel 身份验证检索用户对象还是手动实例化它?
  • @fakemeta 我只是想获得开箱即用的起始页。当我尝试登录时,它给了我这个错误。
  • 另外请注意:我认为autoload 块的psr-4 部分的第二行是多余的。

标签: php laravel laravel-5


【解决方案1】:

您需要更新您的 config/auth.php 文件。将'model' => 'App\User' 更改为'model' => 'App\Models\User'

【讨论】:

  • 为我工作。你是怎么发现的?
【解决方案2】:

不要忘记在之前的所有更改后清除缓存

php artisan config:clear

【讨论】:

    【解决方案3】:

    不要忘记更改您的 User.php 命名空间。

    例如如果您的用户模型位于/App/User.php,则第一行应为:

    <?php namespace App;
    

    但是,如果您创建了一个/Models 目录并且您的用户模型现在位于/App/Models/User.php,那么 User.php 的第一行应该引用这个新的命名空间:

    <?php namespace App\Models;
    

    希望对您有所帮助。

    【讨论】:

      【解决方案4】:

      * 注意:我的策略在技术上可能是“不正确的”,因为它污染了全局命名空间。但是,我无法让其他答案正常工作,并且仅作为最后的手段发布。 *

      我这样做的方式与其他答案略有不同。我也在使用 Laravel 5。

      1) 我创建了 app/Models 目录并将 User.php 移到该目录中。

      2) 我通过删除顶部的命名空间来修改 /app/Models/User.php。这可能会污染全局命名空间,但这是我让它工作的唯一方法。

      <?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) 我在 composer.json 的自动加载部分添加了“app/Models”:

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

      4)我修改auth.php如下:

      /*
      |--------------------------------------------------------------------------
      | 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',
      

      作为参考,这是我的 users 表迁移:

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

      这是我的 UsersTableSeeder.php 文件:

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

      完成所有这些更新后,我在命令行上运行:

      composer dump-autoload 
      php artisan migrate --seed
      

      希望这会有所帮助!

      【讨论】:

      • “这可能会污染全局命名空间,但这是我让它工作的唯一方法。”如果您遵循其他答案,包括我的答案,则不会。
      • 我理解你的反对意见。在发布自己的答案之前,我已经尝试过其他答案,但无法让它们起作用。我会修改我的答案,建议人们在我之前尝试其他技术。
      【解决方案5】:

      别忘了改变

      use App\User;
      

      use App\Models\User;
      

      在 AuthController.php 中

      【讨论】:

        【解决方案6】:

        如果您的模型在特定文件夹中,那么您需要运行

        composer dump-autoload 
        

        刷新:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-11
          • 2015-04-13
          • 1970-01-01
          • 1970-01-01
          • 2016-04-01
          • 1970-01-01
          相关资源
          最近更新 更多