/** * Создание или сохранение продукта * @param Request $request * @return string */ public function postSave(Request $request) { $post_fields_arr = $request->all(); $validator = \Validator::make($post_fields_arr, ['name' => 'required|max:255', 'description' => 'max:5000']); if ($validator->fails()) { return 'Невалидные данные'; } $post_fields_arr['user_id'] = $this->user->id; $post_fields_arr['supplier_id'] = \Input::get('supplier_id'); $product_id = 0; if (isset($post_fields_arr['id']) and intval($post_fields_arr['id']) > 0) { $product_id = intval($post_fields_arr['id']); } if ($product_id) { $product = \App\Models\ProductModel::find($post_fields_arr['id']); if (!$product) { return 'Ошибка: нет виджета с таким ID - ' . $post_fields_arr['id']; } $product->name = $post_fields_arr['name']; $product->description = $post_fields_arr['description']; $product->save(); } else { $product = \App\Models\ProductModel::create($post_fields_arr); } if (isset($post_fields_arr['categories_ids']) and !empty($post_fields_arr['categories_ids'])) { $product->categories()->attach($post_fields_arr['categories_ids']); } if (isset($post_fields_arr['attributes']) and !empty($post_fields_arr['attributes'])) { foreach ($post_fields_arr['attributes'] as $attribute_code => $attribute_value) { $attribute_id = intval(substr($attribute_code, 5)); echo substr($attribute_code, 5); $attribute = \App\Models\AttributeValueModel::where('product_id', '=', $product->id)->where('attribute_id', '=', $attribute_id)->first(); if ($attribute) { $attribute->value = $attribute_value; $attribute->save(); } else { \App\Models\AttributeValueModel::create(['product_id' => $product->id, 'attribute_id' => $attribute_id, 'value' => $attribute_value]); } } } if (isset($post_fields_arr['prices']) and !empty($post_fields_arr['prices'])) { foreach ($post_fields_arr['prices'] as $price_code => $price_value) { \App\Models\PricingGridModel::setPriceByPriceCode($price_code, $price_value, $product->id); } } }