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();