【发布时间】:2020-02-10 02:31:01
【问题描述】:
我尝试在 laravel 中创建 many-to-many 与 customer 和 shop 的链接,但卡在此错误 (errno: 150 "Foreign key constraint is incorrectly formed") 中,但仍然无法弄清楚。
这是我的customers 表
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('shop_id');
$table->string('name');
$table->string('email');
$table->string('phone');
$table->timestamps();
$table->foreign('shop_id')->references('id')->on('shop');
});
这是我的shops 表
Schema::create('shops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('customer_id');
$table->string('name');
$table->timestamps();
$table->foreign('customer_id')->references('id')->on('customer');
});
我的Shop 模特
protected $fillable = ['name'];
public function customer()
{
return $this->belongsToMany(\App\Customer::class);
}
我的Customer 模特
protected $fillable = ['name', 'email', 'phone'];
public function shop()
{
return $this->belongsToMany(\App\Shop::class);
}
有什么帮助吗?在此先感谢....
【问题讨论】:
标签: php laravel eloquent foreign-keys eloquent-relationship