【发布时间】:2021-11-05 22:27:19
【问题描述】:
我有一个这样的父表:
俱乐部
| id | name | budget |
|---|---|---|
| 1 | Arsenal | 90 |
| 2 | Chelsea | 150 |
| 3 | Man City | 135 |
| 4 | Man Utd | 140 |
| 5 | Tottenham | 87 |
还有一个像这样的子表
玩家
| id | club_id | name | position |
|---|---|---|---|
| 1 | 3 | Grealish | LM |
| 2 | 3 | Sterling | LW |
| 3 | 3 | Haaland | ST |
| 4 | 1 | Dybala | ST |
| 5 | 1 | Casemiro | DM |
| 6 | 4 | Fred | DM |
| 7 | 2 | Mbappe | ST |
| 8 | 2 | Hazard | LW |
| 9 | 4 | Varane | DM |
club_id 是引用 clubs 表 id 列的外键
俱乐部模式
public function players()
{
return $this->hasMany('App\Models\Player', 'club_id', 'id');
}
玩家模型
public function club()
{
return $this->belongsTo('App\Models\Club', 'club_id', 'id');
}
我有一个数组输入和逻辑来创建这样的俱乐部和球员:
// the array create input will contain the club and all the players that is related to the club
$createInput = array(
'title' => 'Liverpool',
'budget' => '70'
'players' => [
[
'name' => 'Handerson',
'Position' => 'CM'
],
[
'name' => 'Milner',
'Position' => 'LW'
]
]
);
$club = new Club;
$club->name = $createInput['name];
$club->budget = $createInput['name];
$club->save();
foreach($createInput['players'] as $playerInput){
$player = new Player;
$player->name = $playerInput['name'];
$player->position = $playerInput['position'];
$player->save();
}]
我有一个数组输入和逻辑来更新俱乐部和球员,如下所示:
// the array update input will contain the club and all the players that are related to the club
$updateInput = array(
'id'. => 1,
'budget' => '50',
'players' => [
[
'id' => '4',
'name' => 'Dybala',
'Position' => 'ST'
],
[
'name' => 'Sane',
'Position' => 'LW'
],
[
'name' => 'De Ligt',
'Position' => 'DF'
]
]
);
$club = Club::find($updateInput['id']);
if(isset($updateInput['name'])) $club->name = $updateInput['name'];
if(isset($updateInput['budget'])) $club->budget = $updateInput['budget'];
$club->save();
$existingIds = [];
foreach($updateInput['players'] as $playerInput){
if(isset($playerInput['id'])) $player = Player::find($playerInput['id']);
else $player = new Player;
if(isset($updateInput['name'])) $player->name = $playerInput['name'];
if(isset($updateInput['position'])) $player->name = $playerInput['position'];
$player->save();
$existingIds[] = $player->id;
}
//if the player id is not in the input data then delete it
DB::table('players')->whereNotIn('id', $existingIds)->where('club_id', $club->id)->delete();
我打算通过使用 sync 和 createorupdate 方法来简化我的代码,但我不确定哪个更适合插入/更新/删除连接到俱乐部表的相关球员数据?
【问题讨论】:
标签: php laravel eloquent one-to-many laravel-relations