Laravel:使用sync 或updateOrCreate 来更新parent table 和child table?

Laravel: Use sync or updateOrCreate for updating parent table and child table?

我有一个 parent table 这样的:

俱乐部

id name budget
1 Arsenal 90
2 Chelsea 150
3 Man City 135
4 Man Utd 140
5 Tottenham 87

还有一个像这样的childtable

玩家

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是指俱乐部tableid列的外键

俱乐部模式

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 方法来简化我的代码,但我不确定哪个更适合 table 连接到的 inserting/updating/deleting 相关玩家数据俱乐部 table?

你有一个明确的关系那么为什么不使用它:

当您创建 新记录时:

$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();