Laravel db:seed 尝试播种错误 table

Laravel db:seed attempts to seed wrong table

我在用户和 nextOfKin 模型之间建立了这样的关系

User.php

public function nextOfKin()
{
    return $this->hasOne(NextOfKin::class, 'user_id');
}

NextOfKin.php

public function user()
{
    return $this->belongsTo(User::class);
}

我已经使用迁移创建了 usersnext_of_kins table,并希望使用数据库播种将数据添加到 table。当我 运行 我的 UsersTableSeeder

public function run()
{
    factory(App\User::class)->create()->each(function ($user) {
        $user->nextOfKin()->save(factory(App\NextOfKin::class)->make());
    });
}

它returns一个错误

Illuminate\Database\QueryException : SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "next_of_kin" does not exist LINE 1: insert into "next_of_kin" ("user_id", "firstname", "lastname... ^ (SQL: insert into "next_of_kin" ("user_id", "firstname", "lastname", "relationship", "address", "state", "country", "updated_at", "created_at") values (1, Willie, McCullough, family, 3334 B ridget Cliffs, Michigan, Micronesia, 2019-07-03 13:35:48, 2019-07-03 13:35:48) returning "id")

为什么迁移尝试插入 next_of_kin 而不是 next_of_kins table?用户 table 播种成功。如何解决?

在模型中,您可以指定table使用什么:

class NextOfKin extends Model
{
    protected $table= 'next_of_kins'; 
    ...
}