创建表后,我在迁移期间运行了一些播种机。这是我的迁移文件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' => '$2y$10$/wYQaaaaaaagrtyh64gbdt4yuhr32l4VmFHI.sINMR/9LXsj1MTy',
]);
}
}
据我所知,播种机之间没有真正的区别,但是UsersSeeder在InstitutionsSeeder工作正常的情况下尝试实例化类时迁移失败。
为什么不UsersSeeder工作?
天涯尽头无女友
catspeake