我在我以前的web应用程序中使用了数据库事务的所有方法的Larvel 5.0,它工作得很好,我们真的很喜欢它,因为这个应用程序帮助我比我们估计的更多
所以我们通过使用这个框架的最新版本创建了另一个web应用程序,并使用了相同的数据库结构,但最终它不会为我和另一个工作。我有更多的人在一些教程网站上寻求任何帮助,但还没有得到任何解决方案,所以我录制了这个视频,以确定这个情况。
问题:我的问题我已经禁用了(//commit())方法所有的数据仍然可以插入到数据库
final function Add()
{
if ($this->request->isMethod('post')) {
//No you will see this method use with Try Catch and testing again
//DB::beginTransaction(); // Ihave testing with outside of try and inside again
Try{
DB::beginTransaction();
$cats = new Cat();
$catD = new CategoryDescriptions();
$cats->parent_id = $this->request->input('category_id');
$cats->status = ($this->request->input('status')) ? $this->request->input('status') : 0;
if (($res['result'] = $cats->save())== true) {
$catD->category_id = $cats->id;
$catD->language_id = 1;
$catD->name = $this->request->input('en_name');
if (($res['result'] = $catD->save()) === true) {
$catD2 = new CategoryDescriptions();
$catD2->category_id = $cats->id;
$catD2->language_id = 2;
$catD2->name = $this->request->input('kh_name');
$res['result'] = $catD2->save();
}
}
if(!empty($res)) {
//DB::commit();
}
return [$res,($res['result'] = $catD->save())];
}catch('Exception $e){ // I have already try to use Exception $e without backslash
DB::rollback();
}
}
$cat = Cat::with(['CategoryDescriptions', 'children'])->where('status', 1)->get();
return view('admin.categories.add', ['cat' => $cat]);
}
你可以看看我的视频。
查看我的视频
我不知道为什么你的代码不工作。但你可以尝试用这个代码,我认为它会工作。Laravel事务文档
try{
DB::transaction(function)use(/*your variables*/){
// your code
});
}catch('PDOException $exception){
//debug
}
如果发生任何异常,它将自动回滚。如果你想要手动回滚,那么在事务内部你可以根据你的逻辑抛出一个手动异常。