filter()->get()->toArray(); $goodsIds = array_column($goods, 'id'); // 状态变更时间查询,日志 $day = date('Y-m-d'); //早上7点之前是昨天,7点之后是今天 $goodsSkus = GoodsSku::query() ->whereIn('goods_id', $goodsIds) ->filter() ->with(['goods' => function ($query) { $query->with(['type:id,name', 'brand:id,name']); }]) ->with(['daily' => function ($query) use ($day){ $query->where('day', $day)->with(['daily:id,sku_id,day,arrived_today_num,loss_num,inventory']); }]) ->paginate(); return GoodsSkuResource::collection($goodsSkus); } public function store(Request $request) { $validator = Validator::make($request->all(), [ 'title' => ['required', 'string', 'max:255'], 'img_url' => ['required', 'string', 'max:255'], 'type_id' => ['required', 'integer', 'exists:goods_types,id'], 'brand_id' => ['integer', 'exists:goods_brands,id'], 'goods_code' => ['required', 'alpha_dash', 'max:32', 'unique:goods,goods_code'], 'skus' => ['required', 'array'], 'skus.*.title' => ['required', 'string', 'max:255'], 'skus.*.sku_code' => ['required', 'distinct', 'alpha_dash', 'max:32'], 'skus.*.status' => ['required', 'integer', Rule::in([0, 1, 2])], 'skus.*.num' => ['required', 'integer', 'max:10'], 'skus.*.cost' => ['required', 'numeric', 'max:10'], ]); if ($validator->fails()) { $this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); return response($this->res, $this->res['httpCode']); } DB::beginTransaction(); try { $goods = new Goods(); $goods->title = $request->title; $goods->img_url = $request->img_url; $goods->type_id = $request->type_id; $goods->brand_id = $request->brand_id; $goods->goods_code = $request->goods_code; $goods->save(); $goodsSkus = []; foreach ($request->skus as $item) { $item['goods_id'] = $goods->id; $goodsSkus[] = $item; } $goods->skus()->createMany($goodsSkus); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); $this->res = [ 'httpCode' => 400, 'errorCode' => 400416, 'errorMessage' => $exception->getMessage(), ]; } return response($this->res, $this->res['httpCode']); } }