laravel框架中的MySQL事务处理

整了半天没明白事务为什么会失败!最后还是解决掉了,记录一下,以防忘记,可以参考参考!!!
public function index()
{
    $UserModel = new User(); 
    DB::beginTransaction();  //开启事务
    $o = $UserModel->where(['id' => 2])->update(['type' => 2]);//这个执行成功

    $b = $UserModel->where(['id' => 1])->update(['type' => 2]);//这个执行失败

    if ($b !== true || $o !== true) {//失败则提示信息
        exit("update  error");
    } else {
        DB::commit(); //成功则 提交事务
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39616995/article/details/82911292