productSkus(); if ($request->filled('key')) { $query->where('name', 'like', '%'.$request->input('key').'%'); } $list = $query->paginate(); return $list; } public function create($store_id) { $store = Store::findOrFail($store_id); $form = Form::make(); $form->select('product', '商品')->options(ProductSku::class)->ajax('api/product-skus'); $form->switch('status', '上架')->default(1); return (new Content())->title('新增')->body($form); } public function store($store_id, Request $request) { $store = Store::findOrFail($store_id); $product_id = $request->input('product'); if ($product_id) { $product = $store->productSkus()->find($product_id); if ($product) { $store->productSkus()->updateExistingPivot($product_id, $request->only(['status'])); } else { $store->productSkus()->attach([ $product_id => $request->only(['status']) ]); } } return $this->sendResponse($this->response()->success(trans('admin.save_succeeded'))); } public function update($store_id, $id, Request $request) { $store = Store::findOrFail($store_id); $product = $store->productSkus()->wherePivot('id', $id)->firstOrFail(); $store->productSkus()->updateExistingPivot($product->id, $request->only(['status'])); return $this->sendResponse($this->response()->success(trans('admin.update_succeeded'))); } public function destroy($store_id, $id) { $store = Store::findOrFail($store_id); $product = $store->productSkus()->wherePivot('id', $id)->firstOrFail(); // 删除库存记录 $store->stockLogs()->where('product_sku_id', $product->id)->delete(); // 删除商品关联 $store->productSkus()->wherePivot('id', $id)->detach(); return $this->sendResponse($this->response()->success(trans('admin.delete_succeeded'))); } }