我有一个Match模型和一个MatchCompetitor模型。 MatchCompetitor模型是match_competitors表的关联模型,具有match_id,competitor_type,competitor_id和side_number字段。对于Match模型,我定义了以下关系。但是,当我尝试保存竞争对手时,并没有保存match_id。
public function competitors()
{
return $this->morphToMany(MatchCompetitor::class, 'competitor', 'match_competitors', null, 'competitor_id');
}
要保存我尝试过的关系。
$matchObject->competitors()->attach($player1, ['side_number' => $sideNumber]);
我得到可怕的Not Null约束失败,但应该通过该关系应用match_id。
对于要保存的SQL,这样显示是错误的,因为它应该是App \ Models \ Player,并且应该能够为该表填写match_id。
#sql: "insert into "match_competitors" ("competitor_id", "competitor_type", "side_number") values (?, ?, ?)"
#bindings: array:3 [
0 => 1
1 => "App\Models\Match"
2 => 0
]