博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel事务
阅读量:4111 次
发布时间:2019-05-25

本文共 2914 字,大约阅读时间需要 9 分钟。

if ($result) {            //修改详情            $goodsDetails = GoodsDetails::query()->firstWhere('goods_id',$good->id);        //如果查询成功返回一个Model,如果失败,返回一个null            if(empty($goodsDetails)){                return $this->status('修改商品失败', '', 400);            }            $update = $goodsDetails->update($request->only(['picture','video','sort','content']));//返回一个 bool 或者 int (updated record更新的条目)            if(!$update){                return $this->status('修改商品失败', '', 400);            }            return $this->status('修改商品成功', '', 200);        } else {            return $this->status('修改商品失败', '', 400);        }//添加详情            $goodsDetails = GoodsDetails::query()                ->create(array_merge(['goods_id'=>$good->id],$request->only(['picture','video','sort','content'])));        //返回Model或者null            if(empty($goodsDetails)){                $good->delete();        //返回bool或者null(不存在)                return $this->status('商品添加失败', '', 400);            }            return $this->status('商品添加成功', '', 200);DB::transaction(function()use($request){            //$insertGetId = DB::table('goods')->insertGetId(array_merge([],$request->only(['goods_type_id','goods_name','goods_thumbnail','price','stock','status'])));            //throw new \Exception('error');//抛出异常会回滚            //$insert = DB::table('goods_details')->insert(array_merge(['goods_id' => $insertGetId], $request->only(['picture','video','sort','content'])));            $model = Goods::query()->create(array_merge([], $request->only(['goods_type_id', 'goods_name', 'goods_thumbnail', 'price', 'stock', 'status'])));            if(!empty($model)){                //throw new Exception('商品添加失败');//Model操作抛出异常会卡住?;            }            $goodsDetails = GoodsDetails::query()->create(array_merge(['id'=>1,'goods_id' => $model->getAttribute('id')], $request->only(['picture','video','sort','content'])));            DB::rollBack();//Model如果想要回滚必须配合rollBack        });//使用DB::transaction,如果在闭包中抛出异常可以自动回滚//使用DB:begin 可以自由控制rollback或者commit//事务最佳实践$code = 200;        try {            DB::transaction(function () use ($request) {                $goods = Goods::query()->create(array_merge([], $request->only(['goods_type_id', 'goods_name', 'goods_thumbnail', 'price', 'stock', 'status'])));                if (empty($goods)) {                    $code = 400;                    throw new \Exception('商品添加失败');                }                $goodsDetails = GoodsDetails::query()->create(array_merge(['id' => 1, 'goods_id' => $goods->getAttribute('id')], $request->only(['picture', 'video', 'sort', 'content'])));                if (!empty($goodsDetails)) {                    $code = 400;                    throw new \Exception('商品详情添加失败');                }            });        }catch (\Exception $e){            return $this->status($e->getMessage(), '', $code);        }        return $this->status('商品添加成功', '', $code);

转载地址:http://blrsi.baihongyu.com/

你可能感兴趣的文章
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
OpenFeign学习(七):Spring Cloud OpenFeign的使用
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
查找最大值最小值
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>