Php Laravel 5.3019数据库事务处理方法是否有效?

Php Laravel 5.3019数据库事务处理方法是否有效?,php,laravel-5,eloquent,laravel-5.3,illuminate-container,Php,Laravel 5,Eloquent,Laravel 5.3,Illuminate Container,我在以前的web应用程序中使用了Larvel 5.0和数据库事务处理的所有方法,它也可以工作,我们非常喜欢它,因为这个应用程序对我的帮助比我们估计的要大得多 因此,我们使用这个框架的最新版本创建了另一个Web应用程序,并使用了相同的数据库结构,但最终它对我和另一个应用程序都不起作用。 我有更多的人在一些toturial网站上发帖询问任何belp,但还没有得到任何解决方案,所以我录制了这段视频,以确定这起案件 问题:我的问题我已禁用(//commit())方法,所有数据仍可插入数据库 final

我在以前的web应用程序中使用了Larvel 5.0和数据库事务处理的所有方法,它也可以工作,我们非常喜欢它,因为这个应用程序对我的帮助比我们估计的要大得多

因此,我们使用这个框架的最新版本创建了另一个Web应用程序,并使用了相同的数据库结构,但最终它对我和另一个应用程序都不起作用。 我有更多的人在一些toturial网站上发帖询问任何belp,但还没有得到任何解决方案,所以我录制了这段视频,以确定这起案件

问题:我的问题我已禁用(//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]);
    }
你可以查看我的视频来看看


我不知道你的代码为什么不起作用。但是你可以试试这个代码,我想它会有用的


如果发生任何异常,它将自动回滚。若您想要手动回滚,那个么在事务内部您可以根据您的逻辑抛出一个手动异常

那么到底是什么问题呢?“它对我不起作用”并不是一个真正的答案,你的视频也没有说太多。如果你看到了所有的代码行,你应该理解,因为我在“不要将数据插入数据库”中禁用了commit()方法。如果你不提交,那么数据就不应该保存。数据保存是否有问题?是的,即使我禁用或启用或使用回滚方法而不是提交()来保存所有数据,为什么?有什么解决方案适合我吗?那就是用我们的数据存储引擎工作并确保
try{
     DB::transaction(function)use(/*your variables*/){

       // your code
   });
 }catch(\PDOException $exception){
    //debug
}