public function getPropertiesAttribute() { $categoryProperties = CategoryProperty::select('id', 'category_id', 'name', 'type')->where('category_id', $this->category_id)->get()->toArray(); foreach ($categoryProperties as $k => &$v) { $v['values'] = GoodsCategoryProperty::select('id', 'value')->where('goods_id', $this->id)->where('category_property_id', $v['id'])->get()->toArray(); } return $categoryProperties; }
public function update(Request $requests, $id) { $params = $requests->all(); $goods = Goods::find($id); if ($goods != null) { $properties = []; foreach ($params as $k => $v) { $pos = strpos($k, 'property_'); if ($pos !== false) { $id = substr($k, strlen('property_'), strlen($k)); if (is_numeric($id)) { $properties[$id] = $v; unset($params[$k]); } } } $params['category_id'] = $params['category']; unset($params['category']); unset($params['_token']); $images = $params['images']; unset($params['images']); unset($params['uploadImages']); if ($requests->hasFile('coverImage')) { $file = $requests->file('coverImage'); $fileName = time() . '.' . $file->getClientOriginalExtension(); $file->move(base_path() . '/public/upload', $fileName); $params['cover'] = '/upload/' . $fileName; } unset($params['coverImage']); if ($requests->hasFile('evaluationPersonImage')) { $file = $requests->file('evaluationPersonImage'); $fileName = md5(uniqid()) . '.' . $file->getClientOriginalExtension(); $file->move(base_path() . '/public/upload', $fileName); $params['evaluation_person_image'] = '/upload/' . $fileName; } unset($params['evaluationPersonImage']); $params['goods_description'] = $params['description']; unset($params['description']); $activityClassification = $params['activityClassification']; unset($params['activityClassification']); $freePost = $params['freePost']; unset($params['freePost']); foreach ($params as $n => $p) { $goods->{$n} = $p; } $goods->save(); ActivityClassificationGoods::where('goods_id', $goods->id)->delete(); if ($activityClassification != -1) { ActivityClassificationGoods::create(['activity_classification_id' => $activityClassification, 'goods_id' => $goods->id]); } FreePostGoods::where('goods_id', $goods->id)->delete(); if ($freePost != -1) { FreePostGoods::create(['free_posts_id' => $freePost, 'goods_id' => $goods->id]); } GoodsCategoryProperty::where('goods_id', $goods->id)->delete(); foreach ($properties as $key => $value) { $arr = explode(',', $value); foreach ($arr as $i) { GoodsCategoryProperty::create(['category_property_id' => $key, 'goods_id' => $goods->id, 'value' => $i]); } } GoodsImages::where('goods_id', $goods->id)->delete(); $len = strlen($images); if ($len > 0) { $images = substr($images, 0, $len - 1); $images = explode(',', $images); foreach ($images as $i) { $goodsImage = new GoodsImages(); $goodsImage->goods_id = $goods->id; $goodsImage->image_id = $i; $goodsImage->save(); } } } return redirect()->action('Admin\\GoodsController@show'); }