Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel 5.3中回滚事务?_Php_Laravel_Transactions - Fatal编程技术网

Php 如何在Laravel 5.3中回滚事务?

Php 如何在Laravel 5.3中回滚事务?,php,laravel,transactions,Php,Laravel,Transactions,我正在尝试使用Laravel5.3创建一个演示积垢。我编写了一个控制器的方法来处理更新。此方法应始终启动事务,并始终回滚更改。因此,更改永远不会提交到数据库中 下面是我的方法的样子 public function update($id, Request $request) { DB::beginTransaction(); $this->affirm($request); $biography = Biography::findOrFail($id); $d

我正在尝试使用Laravel5.3创建一个演示积垢。我编写了一个控制器的方法来处理更新。此方法应始终启动事务,并始终回滚更改。因此,更改永远不会提交到数据库中

下面是我的方法的样子

public function update($id, Request $request)
{
    DB::beginTransaction();
    $this->affirm($request);
    $biography = Biography::findOrFail($id);
    $data = $request->all();
    $biography->update($data);

    Session::flash('success_message', 'Biography was updated! However, because this is a demo the records are not persisted to the database.');
    DB::rollBack();

    return redirect()->route('demo.index');
}

不幸的是,每次更新都会被提交。如何正确开始事务,然后回滚更改?

如果提交前的代码引发异常,您可以回滚

public function update($id, Request $request)
{
DB::beginTransaction();
try{
$this->affirm($request);
$biography = Biography::findOrFail($id);
$data = $request->all();
$biography->update($data);

Session::flash('success_message', 'Biography was updated! However, because this is a demo the records are not persisted to the database.');
//if there is not error/exception in the above code, it'll commit
DB::commit();
return redirect()->route('demo.index');
} catch(\Exception $e){
//if there is an error/exception in the above code before commit, it'll rollback
DB::rollBack();
return $e->getMessage();
}

}
你可以查一下
下面是您可以回滚的

,以防提交前的代码引发异常

public function update($id, Request $request)
{
DB::beginTransaction();
try{
$this->affirm($request);
$biography = Biography::findOrFail($id);
$data = $request->all();
$biography->update($data);

Session::flash('success_message', 'Biography was updated! However, because this is a demo the records are not persisted to the database.');
//if there is not error/exception in the above code, it'll commit
DB::commit();
return redirect()->route('demo.index');
} catch(\Exception $e){
//if there is an error/exception in the above code before commit, it'll rollback
DB::rollBack();
return $e->getMessage();
}

}
你可以查一下
以下是提交后您不能回滚的问题

?正如我看到的,您正在从数据库获取数据,并使用有说服力的查询进行更新。我不想提交。我想后退。我希望启动一个事务,然后将其回滚。我已经尝试过,发现代码工作正常。你能告诉我你的输出是什么吗?你用的是什么数据库?如果是MySQL,你使用的是哪个引擎?如果你想在更新后立即回滚,那么你为什么要首先更新记录?我不明白。执行多个相关db操作时使用的回滚,如果其中一个操作失败,则所有操作都将自动恢复,我认为您不是这样使用的。提交后您不能回滚吗?正如我看到的,您正在从数据库获取数据,并使用有说服力的查询进行更新。我不想提交。我想后退。我希望启动一个事务,然后将其回滚。我已经尝试过,发现代码工作正常。你能告诉我你的输出是什么吗?你用的是什么数据库?如果是MySQL,你使用的是哪个引擎?如果你想在更新后立即回滚,那么你为什么要首先更新记录?我不明白。在执行多个相关的db操作时使用回滚,如果其中一个操作失败,那么所有操作都将自动恢复,我认为您没有这样使用它。