laravel事务小例子

发生异常则自动回滚,正常则自动提交,示例如下:

DB::connection('vshare')->transaction(function() use($id,$reason,$refuser_id){    

    VideoRefuse::insert(array('video_id'=>$id,'refuser_id'=>$refuser_id,'reason'=>$reason,'created_at'=>time()));

    Program::where('id',$id)->update(array('deleted_flag'=>1,'status'=>4));

    VideoReport::where('video_id',$id)->delete();   

});

 

也可手动使用事务:

开始事务:DB::beginTransaction();回滚事务:DB::rollback();

提交事务:DB::commit();

 

posted @ 2015-11-17 10:34  冰柠檬草  阅读(369)  评论(0编辑  收藏  举报