Blogger Information
Blog 20
fans 0
comment 0
visits 33321
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel中的事务的使用方式
陈文鹏的博客
Original
2840 people have browsed it

<?php 



    public function audit($id)

{

$ttip=DB::table('ttip')->where('id',$id)->first();

$username=$ttip->username;

$user=DB::table('user')->where('username',$username)->first();

$alreleased=$user->alreleased;//更新user表中的数据

$ttBumber=$ttip->ttBumber;//ttip表中的数量

$carryout=$user->carryout;//更新user表中的数据


if($alreleased<$ttBumber)

{

$message="交易失败";

return redirect('/admin')->with('message',$message);

}



DB::beginTransaction(); //开启事物

//扑捉异常

try {

$res=Db::table('ttip')->where("id",$id)->update(["status"=>1]);

$nums=Db::table('user')->where("username",$username)->decrement('subscriptotal',$ttBumber);//递减

$num=Db::table('user')->where("username",$username)->decrement('alreleased',$ttBumber); //递减

$takeaway=DB::table('user')->where('username',$username)->increment('carryout',$ttBumber);//递增

if($res==true && $num==true && $takeaway==true && $nums==true){

DB::commit();//提交保存

$message="交易成功";

return redirect('/admin')->with('message',$message);



}else{

DB::rollBack();

}


} catch (Exception $e) {

// 数据回滚, 当try中的语句抛出异常。

DB::rollBack();


}



}

 ?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post