laravel事务

2020-05-30  本文已影响0人  IT宝哥哥
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);
上一篇下一篇

猜你喜欢

热点阅读