public function update(Request $request, $id) { $messages = ['name.required' => '商品名称不能为空!', 'category_id.required' => '商品分类不能为空!']; $this->validate($request, ['name' => 'required', 'category_id' => 'required'], $messages); $good = Good::find($id); $result = $request->except(['imgs', 'attr_id_list', 'attr_value_list', 'attr_price_list']); //如果checkbox未选中,设置为false $result = isset($request->best) ? $result : array_add($result, 'best', false); $result = isset($request->new) ? $result : array_add($result, 'new', false); $result = isset($request->hot) ? $result : array_add($result, 'hot', false); $result = isset($request->onsale) ? $result : array_add($result, 'onsale', false); $good->update($result); //增加属性 if ($request->attr_id_list) { //先删除原有属性 Good_attr::where('good_id', $id)->delete(); foreach ($request->attr_id_list as $k => $v) { $good_attr = new Good_attr(); $good_attr->good_id = $good->id; $good_attr->attr_id = $v; $good_attr->attr_value = $request->attr_value_list["{$k}"]; $good_attr->attr_price = $request->attr_price_list["{$k}"]; $good_attr->save(); } } //商品相册 if ($request->imgs) { foreach ($request->imgs as $img) { $good_gallery = new Good_gallery(); $good_gallery->good_id = $good->id; $good_gallery->img = $img; $good_gallery->save(); } } return redirect(route('admin.good.index'))->with('info', '编辑商品成功'); }
public function update(Request $request, $id) { $good = Good::find($id); $result = $request->except(['imgs', 'attr_id_list', 'attr_value_list', 'attr_price_list']); $result = isset($result->hot) ? $result : array_add($result, 'hot', 0); $result = isset($result->best) ? $result : array_add($result, 'best', 0); $result = isset($result->new) ? $result : array_add($result, 'new', 0); $result = isset($result->onsale) ? $result : array_add($result, 'onsale', 0); $good->update($result); //增加属性 if ($request->attr_id_list) { //先删除原有属性 Good_attr::where('good_id', $id)->delete(); foreach ($request->attr_id_list as $k => $v) { $good_attr = new Good_attr(); $good_attr->good_id = $good->id; $good_attr->attr_id = $v; $good_attr->attr_value = $request->attr_value_list["{$k}"]; $good_attr->attr_price = $request->attr_price_list["{$k}"]; $good_attr->save(); } } //商品相册 if ($request->imgs) { foreach ($request->imgs as $img) { $good_gallery = new Good_gallery(); $good_gallery->good_id = $good->id; $good_gallery->img = $img; $good_gallery->save(); } } return redirect(route('admin.good.index'))->with('info', '编辑商品成功'); }