1

我有一个这样的父表:

俱乐部

ID 姓名 预算
1 兵工厂 90
2 切尔西 150
3 曼城 135
4 曼联 140
5 托特纳姆热刺 87

还有这样的子表

球员

ID club_id 姓名 位置
1 3 希腊语 LM
2 3 英镑 长宽
3 3 哈兰德 英石
4 1 迪巴拉 英石
5 1 卡塞米罗 DM
6 4 弗雷德 DM
7 2 姆巴佩 英石
8 2 冒险 长宽
9 4 瓦拉内 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 方法来简化我的代码,但我不确定哪个更适合插入/更新/删除连接到俱乐部表的相关球员数据?

4

1 回答 1

0

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

当您创建新记录时:

$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();
于 2021-09-09T09:48:03.380 回答