Laravel Eloquent ORM Transactions

后端 未结 7 2052
闹比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 18:18

    If you want to avoid closures, and happy to use facades, the following keeps things nice and clean:

    try {
        \DB::beginTransaction();
    
        $user = \Auth::user();
        $user->fill($request->all());
        $user->push();
    
        \DB::commit();
    
    } catch (Throwable $e) {
        \DB::rollback();
    }
    

    If any statements fail, commit will never hit, and the transaction won't process.

    0 讨论(0)
提交回复
热议问题