payload['spu_id'] ?? 0; $_skuSpecs = []; $_price = 0; foreach ($input as $id => $value) { $spuSpec = ProductSpuSpec::where('id', $id)->first(); foreach ($spuSpec->items as $item) { if ($item['name'] == $value) { $_skuSpecs[$id] = $item['name']; $_price += $item['value']; } } } //如果存在该规格组合 $skuQuery = ProductSku::where('spu_id', $spuId); foreach ($_skuSpecs as $id=> $spec) { $skuQuery->where('specs->'.$id, $spec); } if ($skuQuery->first()) { return $this->response()->error(__('admin_message.forms.add_sku.errors.has_specs')); } else { $spu = ProductSpu::findOrFail($spuId); ProductSku::create([ 'spu_id' => $spu->id, 'name' => trim($spu->name.' '.implode(' ', $_skuSpecs)), 'subtitle' => $spu->subtitle, 'category_id' => $spu->category_id, 'cover' => $spu->cover, 'images' => $spu->images, 'description' => $spu->description, 'sell_price' => bcadd($spu->sell_price, $_price), 'market_price' => $spu->market_price, 'cost_price' => $spu->cost_price, 'vip_price' => !is_null($spu->vip_price) ? bcadd($spu->vip_price, $_price) : null, 'media' => $spu->media, 'weight' => $spu->weight, 'attrs' => $spu->attrs, 'specs' => $_skuSpecs, 'stock' => $spu->stock, 'sales' => $spu->sales, 'buynote_id' => $spu->buynote_id, ]); } } return $this->response() ->success(__('admin.update_succeeded')) ->refresh(); } /** * Build a form here. */ public function form() { $spuId = $this->payload['spu_id'] ?? 0; $spu = ProductSpu::findOrFail($spuId); foreach ($spu->specs as $spec) { $this->select($spec->id, $spec->name)->options(function () use ($spec) { return collect($spec->items)->pluck('name', 'name'); })->required(); } $this->disableResetButton(); } /** * The data of the form. * * @return array */ public function default() { return [ // 'name' => 'John Doe', // 'email' => 'John.Doe@gmail.com', ]; } }