AブロックとBブロックがあるトーナメントでそれぞれ1〜8番の枠がある。
エントリーが10人で残りの6枠が埋まらなかったので、その6枠にダミーのデータを挿入する処理を書いた。
$blockList = DB::table('players')->select('block_id')->distinct()->get();
$blockListの中身にデータがあるかチェック。
if (!is_array($blockList) || empty($blockList)) {
print_r("blockList is empty.\n");
} else {
//ここにトランザクションの処理を書いていく。
}
トランザクションは以下のように書く。
try {
DB::transaction(function() use($blockList) {
if (!DB::table('players')->insert($record)) {
throw new \Exception();
}
});
} catch (\Exception $e) {
print_r("error: Failed to save data.\n");
}