我有一个这样的父表:
<表类>
id
名称
预算
tbody><<tr>1 阿森纳 90 2切尔西 150 3 曼城 135 4曼联 140 5托特纳姆 87 表类>
你有一个定义的关系,为什么不使用它:
当创建时一个新记录:$club = new Club;
$club->name = $createInput['name];
$club->budget = $createInput['name];
$club->save();
// club_id automatic attach with players
$club->players()->createMany( $createInput['players'] );
,有关系的记录:
$club = Club::find($updateInput['id']);
if(isset($updateInput['name'])) $club->name = $updateInput['name'];
if(isset($updateInput['budget'])) $club->budget = $updateInput['budget'];
$club->save();
foreach($updateInput['players'] as $playerInput){
$playerData = $club->players()->updateOrCreate(
['id' => $playerInput['id']],
['name' => $playerInput['name'], 'position' => $playerInput['position']]
);
}
当你需要删除时具有父级关系的记录:
$club = Club::find( $id_of_club );
$club->players()->delete();