LoginSignup
0
1

More than 5 years have passed since last update.

Laravel5でトランザクション

Posted at

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");
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1