数据库事务不执行回滚?

public function store(PostRequest $request)
    {
        if (is_array($request->get('font_style'))) {
            $request['font_style'] = implode(',', $request->get('font_style'));
        }

        //附加表数据
        $result = \DB::transaction(function () use ($request) {
            try {
                //保存文章
                $post = Post::create($request->except('file', 'me_setting', 'tags'));
                //保存标签
                $post->syncTags($request->get('tags'));
                //保存附加表
                if (!is_null($request->get('me_setting'))) {
                    $tableName = $this->getRetableName($request->get('category_id'));
                    if (\Schema::hasTable($tableName)) {
                        //
                        $me_setting_array=$request->get('me_setting');
                        foreach ($me_setting_array as $k=>$v){
                            if (is_array($v)){
                                $me_setting_array[$k]=implode(',',$v);
                            }
                        }
                        \DB::table($tableName)->insert(array_merge(['post_id' => $post->id], $request->get('me_setting')));
                    }
                }
            } catch (\Exception $e) {
                return $e;
            }
        });
        if (is_null($result)) {
            return redirect()->route('post.index')->with('successMsg', '文章创建成功!');
        }
        return back()->withInput()->withErrors('文章创建失败');
    }

为什么使用事务的时候后面附加表的数据保存失败了,文章表的数据还能保存进去?求大神赐教,感谢!

xtn
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

因为你在内部 catch 了异常,去掉 try...catch... 试试看。

5年前 评论
讨论数量: 2

因为你在内部 catch 了异常,去掉 try...catch... 试试看。

5年前 评论
xtn

@Wi1dcard 好呢 非常感谢 我试试

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!