PHP 簡單的事務用法實例+(Exception 錯誤自定義;拋出:laraval實現)

 1 <?php  2 
 3 namespace App\Http\Controllers;  4 
 5 use Illuminate\Http\Request;  6 
 7 use Illuminate\Support\Facades\DB;  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 }
相關文章
相關標籤/搜索