Laravel Eloquent ORM Transactions

后端 未结 7 2050
闹比i
闹比i 2020-12-22 17:30

The Eloquent ORM is quite nice, though I\'m wondering if there is an easy way to setup MySQL transactions using innoDB in the same fashion as PDO, or if I would have to exte

7条回答
  •  囚心锁ツ
    2020-12-22 17:57

    If you don't like anonymous functions:

    try {
        DB::connection()->pdo->beginTransaction();
        // database queries here
        DB::connection()->pdo->commit();
    } catch (\PDOException $e) {
        // Woopsy
        DB::connection()->pdo->rollBack();
    }
    

    Update: For laravel 4, the pdo object isn't public anymore so:

    try {
        DB::beginTransaction();
        // database queries here
        DB::commit();
    } catch (\PDOException $e) {
        // Woopsy
        DB::rollBack();
    }
    

提交回复
热议问题