【问题标题】:Class 'User' not found找不到类“用户”
【发布时间】:2015-08-26 18:25:47
【问题描述】:

所以我在迁移数据库后尝试使用基本的php artisan db:seed,但它在 cmd -[Symfony\Component\Debug\Exception\FatalErrorException] Class 'User' not found 中不断返回标题错误

我尝试过的事情

  • 更新类后的 php dump-autoload
  • 运行db:seed函数之前的php dump-autoload
  • 回滚迁移,然后重新运行它
  • 回滚迁移,然后使用 --seed 语法重新运行它
  • 更改“用户”文件的命名空间

以下是迁移

<?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('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

我相信这里的一切都是正确的,现在这里是用户类

<?php namespace App;

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 = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

}

现在最后是最重要的数据库播种器

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        // $this->call('UserTableSeeder');
        $this->call('UserTableSeeder');

        Model::reguard();
    }
}

class UserTableSeeder extends Seeder
{
    public function run()
    {

        DB::table('users')->delete();

        User::create(['email' => 'John@doe.com']);

    }
}

这就是我的完整语法,如果需要更多文件,请请求它们,我会更新我的问题。

【问题讨论】:

    标签: php laravel-5 laravel-artisan


    【解决方案1】:

    在根命名空间中的DatabaseSeeder 中,您调用类User。因此它尝试加载类User。但是,您的类 User 的定义位于命名空间 App 中。因此,您应该在 DatabaseSeeder 中使用 App\User 或在文件顶部添加 use App\User;

    DatabaseSeeder

    <?php
    
    use App\User;
    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    
    class DatabaseSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            Model::unguard();
    
            // $this->call('UserTableSeeder');
            $this->call('UserTableSeeder');
    
            Model::reguard();
        }
    }
    
    class UserTableSeeder extends Seeder
    {
        public function run()
        {
    
            DB::table('users')->delete();
    
            User::create(['email' => 'John@doe.com']);
    
        }
    }
    

    自 Laravel 8+ 起,User 类现在默认存储在 app/Models 目录中;所以不要使用App\User,而是使用上面的App\Models\User

    附言。默认情况下,Laravel 附带一个 User 模型,请使用该模型。如果您删除了该模型,您可以使用 Laravel 提供的后备:

    use Illuminate\Foundation\Auth\User;
    

    在旁注中,我发现对于调试工匠输出非常有用。您应该使用标志-vvv,它会在输出消息中添加极其冗长的信息,包括完整的堆栈跟踪。

    php artisan migrate -vvv
    

    【讨论】:

    • 如果use App\User; 不起作用,那么就这样写use Illuminate\Foundation\Auth\User;
    • @AshwaniPanwar 谢谢你这是我的问题并为我解决了!
    • use Illuminate\Foundation\Auth\User; 是我需要的
    • 感谢@AshwaniPanwar 我必须使用 Laravel 5.8.38 将此行添加到 DatabaseSeeder.phpUsersTableSeeder.php。问候
    猜你喜欢
    • 2014-07-17
    • 2013-03-25
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多