paginate(); return GoodsBrandResource::collection($goodsBrands); } public function store(Request $request) { $validator = Validator::make($request->all(), [ 'names' => 'required|array', 'names.*' => 'required|string|max:255|unique:goods_brands,name', ]); if ($validator->fails()) { $this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); return response($this->res, $this->res['httpCode']); } $goodsBrands = []; foreach ($request->names as $name) { $goodsBrands[] = ['name' => $name]; } $goodsBrand = new GoodsBrand(); if (!$goodsBrand->batchInsert($goodsBrands)) { $this->res = [ 'httpCode' => 500, 'errorCode' => 500500, 'errorMessage' => '批量添加失败', ]; } return response($this->res, $this->res['httpCode']); } public function show($id) { return new GoodsBrandResource(GoodsBrand::query()->find($id)); } public function update($id, Request $request) { $validator = Validator::make($request->all(), [ 'name' => [ 'required', 'string', 'max:255', Rule::unique('goods_brands')->ignore($id), ] ]); if ($validator->fails()) { $this->setValidatorFailResponse($validator->getMessageBag()->getMessages()); return response($this->res, $this->res['httpCode']); } $goodsBrand = GoodsBrand::query()->find($id); $goodsBrand->name = request('name'); $goodsBrand->save(); return new GoodsBrandResource($goodsBrand); } public function destroy($id) { $goodsBrand = GoodsBrand::query()->find($id); try { $goodsBrand->delete(); } catch (\Exception $e) { $this->res = [ 'httpCode' => 500, 'errorCode' => 500416, 'errorMessage' => $e->getMessage(), ]; } return response($this->res, $this->res['httpCode']); } }