Laravel 找不到播种机
Laravel Seeder not found
我是 运行 创建 table 后迁移期间的一些播种者。这是我的迁移文件 create_institutions_table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInstitutionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('institutions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('code');
$table->timestamps();
$table->softDeletes();
});
$seeder = new InstitutionsSeeder();
$seeder->run();
$seeder2 = new UsersSeeder();
$seeder2->run();
Schema::table('users', function (Blueprint $table) {
$table->foreign('institution_id')->references('id')->on('institutions');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('institutions');
}
}
这是InstitutionsSeeder
use Illuminate\Database\Seeder;
class InstitutionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('institutions')->insert([
'name' => 'Institution One',
'code' => 'I1',
]);
}
}
这是UsersSeeder
use Illuminate\Database\Seeder;
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'first_name' => 'Admin',
'last_name' => 'Istrator',
'email' => 'admin@example.com',
'institution_id' => '1',
'password' => 'y$/wYQaaaaaaagrtyh64gbdt4yuhr32l4VmFHI.sINMR/9LXsj1MTy',
]);
}
}
据我所知,播种机之间没有真正的区别,但是在尝试实例化 UsersSeeder
class 时迁移失败,而 InstitutionsSeeder
工作正常。这是我从 php artisan migrate:fresh
命令得到的异常:
Symfony\Component\Debug\Exception\FatalThrowableError : Class 'UsersSeeder' not found
at H:\code\MyProject\database\migrations19_06_17_224612_create_institutions_table.php:27
23|
24| $seeder = new InstitutionsSeeder();
25| $seeder->run();
26|
> 27| $seeder2 = new UsersSeeder();
28| $seeder2->run();
29|
30| Schema::table('users', function (Blueprint $table) {
31| $table->foreign('institution_id')->references('id')->on('institutions');
Exception trace:
1 CreateInstitutionsTable::up()
H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:379
2 Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations\{closure}()
H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:388
Please use the argument -v to see more details.
为什么 UsersSeeder
不起作用?
两种可能的解决方案:
检查您的 class
的命名空间
运行作曲家dump-autoload:composer dump-autoload
(你可以read the docs这里)
每次创建一个新的播种器运行 composer dump-autoload
命令。
之后只是 运行 播种机使用 php artisan db:seed
命令。
希望这会奏效!
我是 运行 创建 table 后迁移期间的一些播种者。这是我的迁移文件 create_institutions_table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateInstitutionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('institutions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('code');
$table->timestamps();
$table->softDeletes();
});
$seeder = new InstitutionsSeeder();
$seeder->run();
$seeder2 = new UsersSeeder();
$seeder2->run();
Schema::table('users', function (Blueprint $table) {
$table->foreign('institution_id')->references('id')->on('institutions');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('institutions');
}
}
这是InstitutionsSeeder
use Illuminate\Database\Seeder;
class InstitutionsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('institutions')->insert([
'name' => 'Institution One',
'code' => 'I1',
]);
}
}
这是UsersSeeder
use Illuminate\Database\Seeder;
class UsersSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'first_name' => 'Admin',
'last_name' => 'Istrator',
'email' => 'admin@example.com',
'institution_id' => '1',
'password' => 'y$/wYQaaaaaaagrtyh64gbdt4yuhr32l4VmFHI.sINMR/9LXsj1MTy',
]);
}
}
据我所知,播种机之间没有真正的区别,但是在尝试实例化 UsersSeeder
class 时迁移失败,而 InstitutionsSeeder
工作正常。这是我从 php artisan migrate:fresh
命令得到的异常:
Symfony\Component\Debug\Exception\FatalThrowableError : Class 'UsersSeeder' not found
at H:\code\MyProject\database\migrations19_06_17_224612_create_institutions_table.php:27
23|
24| $seeder = new InstitutionsSeeder();
25| $seeder->run();
26|
> 27| $seeder2 = new UsersSeeder();
28| $seeder2->run();
29|
30| Schema::table('users', function (Blueprint $table) {
31| $table->foreign('institution_id')->references('id')->on('institutions');
Exception trace:
1 CreateInstitutionsTable::up()
H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:379
2 Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations\{closure}()
H:\code\MyProject\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:388
Please use the argument -v to see more details.
为什么 UsersSeeder
不起作用?
两种可能的解决方案:
检查您的 class
的命名空间
运行作曲家dump-autoload:
composer dump-autoload
(你可以read the docs这里)
每次创建一个新的播种器运行 composer dump-autoload
命令。
之后只是 运行 播种机使用 php artisan db:seed
命令。
希望这会奏效!