public function guardarDatos($request, $id) { $rules = ['name' => 'required', 'price' => 'required', 'id_category' => 'required']; $this->validate($request, $rules); if ($id != null) { $producto = Product::findOrFail($id); } else { $producto = new Product(); } $producto->name = $request->input('name'); $producto->description = $request->input('description'); $producto->price = $request->input('price'); $producto->id_category = $request->input('id_category'); $producto->save(); $colors = Color::orderBy('name', 'asc')->get(); foreach ($colors as $col) { $checkColor = $request->input('color' . $col->id); if (isset($checkColor)) { $products_colors = ProductColor::where('id_color', $col->id)->where('id_product', $producto->id)->first(); if (!isset($products_colors->id)) { $products_colors = new ProductColor(); } $products_colors->id_color = $col->id; $products_colors->id_product = $producto->id; $products_colors->save(); } else { $products_colors = ProductColor::where('id_color', $col->id)->where('id_product', $producto->id)->first(); if (isset($products_colors->id)) { $products_colors->delete(); } } } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate request $validateProductColor = Validator::make($request->get('ProductColor'), ProductColor::$rules); $validationMessages = []; foreach ($request->get('ProductColorTranslation') as $key => $value) { $validateProductColorTranslation = Validator::make($value, ProductColorTranslation::$rules); if ($validateProductColorTranslation->fails()) { $validationMessages = array_merge_recursive($validationMessages, $validateProductColorTranslation->messages()->toArray()); } } if ($validateProductColor->fails() or count($validationMessages) > 0) { $validationMessages = array_merge_recursive($validateProductColor->messages()->toArray(), $validationMessages); return redirect()->back()->withErrors($validationMessages)->withInput(); } // get all languages $languages = Language::all(); // find language default $languageDefault = $languages->where('is_key_language', 1)->first(); if (is_null($languageDefault)) { $languageDefault = $languages->first(); } // sure execute success, if not success rollback DB::transaction(function () use($request, $languageDefault) { $user = $request->user(); // insert ProductColor $productColor = new ProductColor(); $productColor->key = Common::createKeyURL($request->input('ProductColorTranslation.' . $languageDefault->code . '.name')); $productColor->parent_id = $request->input('ProductColor.parent_id'); $productColor->priority = $request->input('ProductColor.priority'); $productColor->is_publish = $request->input('ProductColor.is_publish'); $productColor->created_by = $user->name; $productColor->updated_by = $user->name; $productColor->save(); // save attachments if ($request->input('ProductColor.attachments') != "") { $requestAttachments = explode(',', $request->input('ProductColor.attachments')); $attachments = []; foreach ($requestAttachments as $key => $value) { array_push($attachments, new Attachment(['entry_id' => $productColor->id, 'table_name' => 'product_colors', 'path' => $value, 'priority' => 0, 'is_publish' => 1])); } if (count($attachments) > 0) { $productColor->attachments()->saveMany($attachments); } } // save data languages foreach ($request->get('ProductColorTranslation') as $locale => $value) { $productColor->translateOrNew($locale)->name = $request->input('ProductColorTranslation.' . $locale . '.name'); $productColor->translateOrNew($locale)->summary = $request->input('ProductColorTranslation.' . $locale . '.summary'); $productColor->translateOrNew($locale)->meta_description = $request->input('ProductColorTranslation.' . $locale . '.meta_description'); $productColor->translateOrNew($locale)->meta_keywords = $request->input('ProductColorTranslation.' . $locale . '.meta_keywords'); } $productColor->save(); }); return redirect()->route('admin.productcolors.index'); }
public function saveColor($data, $id = null) { if ($id) { $color = ProductColor::find($id); } else { $color = new ProductColor(); } $color->color = $data['color']; if (array_key_exists('product_id', $data)) { $color->product_id = $data['product_id']; } if (array_key_exists('size_id', $data)) { $color->size_id = $data['size_id']; } $color->save(); return 0; }