laravel事务

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值