Laravel Eloquent ORM Transactions

后端 未结 7 2179
闹比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:14

    If any exception occurs, the transaction will rollback automatically.

    Laravel Basic transaction format

        try{
        DB::beginTransaction();
    
        /* 
        * SQL operation one 
        * SQL operation two
        ..................     
        ..................     
        * SQL operation n */
    
    
        DB::commit();
       /* Transaction successful. */
    }catch(\Exception $e){       
    
        DB::rollback();
        /* Transaction failed. */
    }
    

提交回复
热议问题