Laravel 数据库播种

Laravel DatabaseSeeding

我正在尝试了解 laravel。我不忙于播种我不断收到以下错误:

[ReflectionException]
Class UserTableSeeder does not exist

我做了什么:

用户class

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', 'username', 'email', 'password'];

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

CreateUserTable 迁移:

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

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

UserTableSeeder

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

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB:table('users')->delete();
        User::create(array(
            'name'      => 'Graham Neal',
            'username'  => 'Graham',
            'email'     => 'grahamneal1991@gmail.com',
            'password'  => Hash::make('0258456'),
        ));
    }
}

数据库播种器

    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::class);
        $this->call('UserTableSeeder');

        Model::reguard();
    }
}

在数据库播种机中,我尝试了以下操作:

$this->call('UserTableSeeder');
$this->call('UserTableSeeder::User');
$this->call('App/UserTableSeeder');
$this->call('App/UserTableSeeder::User');

我知道它找不到 class 但我对找出原因一无所知?我搜索了 google 并找到了其他人出错的地方,但我似乎没有同样的错误。希望这不是一个非常愚蠢的问题。

编辑*

如果我执行 composer dump-autoload,我会收到上述错误和以下异常跟踪:

  [RuntimeException]
  Could not scan for classes inside "user" which does not appear to be a file
   nor a folder

这个错误是在我手动添加 class 到 composer.json 文件后出现的。

现在,如果我尝试 php artisan db:seed,我会收到另一个错误:

[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined function table()

首先,确保您的播种器在 database/seed/ 目录中。

然后,使用作曲家的 dump-autoload

确保您的文件命名正确。它应该与 DatabaseSeeder 位于同一文件夹中。