加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP 简单的事务用法实例+(Exception 错误自定义;抛出:laraval

发布时间:2020-12-13 16:06:54 所属栏目:PHP教程 来源:网络整理
导读:1 ? php 2 3 namespace AppHttpControllers; 4 5 use IlluminateHttpRequest; 6 7 use IlluminateSupportFacadesDB; 8 9 10 class ManayController extends Controller 11 { 12 public function index(){ 13 14 try { 15 16 DB:: beginTransaction();
 1 <?php  2 
 3 namespace AppHttpControllers;  4 
 5 use IlluminateHttpRequest;  6 
 7 use IlluminateSupportFacadesDB;  8 
 9 
10 class ManayController extends Controller 11 { 12     public function index(){ 13 
14         try { 15 
16             DB::beginTransaction(); 17 
18             $res1 = DB::table(‘manay‘)->where(‘id‘,‘1‘)->increment(‘manay‘,200); 19 
20             $res2 = DB::table(‘manay‘)->where(‘id‘,‘2‘)->decrement(‘manay‘,200); 21 
22             if($res1==true && $res2==true){ 23 
24                 DB::commit(); 25 
26             }else{ 27 
28                 $this->fun(); 29 
30  } 31 
32         }catch (Exception $e1){ 33 
34               echo "发生错误(代号:". $e1->getCode() . ")"; 35 
36               echo $e1->getMessage(); 37 
38              DB::rollBack(); 39 
40 
41 
42  } 43 
44 
45  } 46 
47     public function fun(){ 48 
49          throw new Exception(‘有错误‘,‘300‘); 50 
51 
52 
53  } 54 
55 
56 
57 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读