【发布时间】:2021-01-26 10:24:18
【问题描述】:
您好,我正在尝试添加两个引用同一个表的外键列。但我被这个错误困住了。我花了很多时间试图找出我的代码的问题。我已经在stackoverflow中浏览了所有可能的重复帖子,但我找不到解决方案。
我的代码很长,所以我在这里提到了重要的部分,如果您需要有关此问题的任何进一步信息,请告诉我。
这是我的外键关系
$table->unsignedBigInteger('country_id');
$table->unsignedBigInteger('city_id');
$table->unsignedBigInteger('work_location');
$table->unsignedBigInteger('citizenship');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('city_id')->references('id')->on('cities');
$table->foreign('work_location')->references('id')->on('cities');
$table->foreign('citizenship')->references('id')->on('countries');
这些是我的主要模特关系
public function city()
{
return $this->belongsTo(City::class, 'city_id');
}
public function country()
{
return $this->belongsTo(Country::class,'country_id');
}
public function citizenship()
{
return $this->belongsTo(Country::class,'citizenship');
}
public function work_location()
{
return $this->belongsTo(City::class, 'work_location');
}
这就是我通过 phpunit 向模型发送数据的方式
'citizenship'=>Country::factory()->create();,
'work_location'=>City::factory()->create();,
'city_id' => City::factory()->create();,
'country_id' => Country::factory()->create(),
【问题讨论】:
标签: php sqlite eloquent laravel-8