/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate request $validateProduct = Validator::make($request->get('Product'), Product::$rules); $validationMessages = []; foreach ($request->get('ProductTranslation') as $key => $value) { $validateProductTranslation = Validator::make($value, ProductTranslation::$rules); if ($validateProductTranslation->fails()) { $validationMessages = array_merge_recursive($validationMessages, $validateProductTranslation->messages()->toArray()); } } if ($validateProduct->fails() or count($validationMessages) > 0) { $validationMessages = array_merge_recursive($validateProduct->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 Product $product = new Product(); $product->key = Common::createKeyURL($request->input('ProductTranslation.' . $languageDefault->code . '.name')); $product->code = $request->input('Product.code'); $product->model = $request->input('Product.model'); $product->producer_id = $request->input('Product.producer_id'); $product->origin = $request->input('Product.origin'); $product->unit = $request->input('Product.unit'); $product->price = $request->input('Product.price'); $product->discount = $request->input('Product.discount'); $product->priority = $request->input('Product.priority'); $product->is_publish = $request->input('Product.is_publish'); $product->created_by = $user->name; $product->updated_by = $user->name; $product->save(); // sync categories if ($request->input('Product.categories') != "") { $categories = explode(",", $request->input('Product.categories')); if (count($categories) > 0) { $product->categories()->attach($categories); } } // sync colors if ($request->input('Product.colors') != "") { $colors = explode(",", $request->input('Product.colors')); if (count($colors) > 0) { $product->colors()->attach($colors); } } // save attachments if ($request->input('Product.attachments') != "") { $requestAttachments = explode(',', $request->input('Product.attachments')); $attachments = []; foreach ($requestAttachments as $key => $value) { array_push($attachments, new Attachment(['entry_id' => $product->id, 'table_name' => 'products', 'path' => $value, 'priority' => 0, 'is_publish' => 1])); } if (count($attachments) > 0) { $product->attachments()->saveMany($attachments); } } // save data languages foreach ($request->get('ProductTranslation') as $locale => $value) { $product->translateOrNew($locale)->name = $request->input('ProductTranslation.' . $locale . '.name'); $product->translateOrNew($locale)->summary = $request->input('ProductTranslation.' . $locale . '.summary'); $product->translateOrNew($locale)->content = $request->input('ProductTranslation.' . $locale . '.content'); $product->translateOrNew($locale)->meta_description = $request->input('ProductTranslation.' . $locale . '.meta_description'); $product->translateOrNew($locale)->meta_keywords = $request->input('ProductTranslation.' . $locale . '.meta_keywords'); } $product->save(); }); return redirect()->route('admin.products.index'); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate request $validateProduct = Validator::make($request->get('Product'), Product::$rules); $validationMessages = []; if ($validateProduct->fails() or count($validationMessages) > 0) { $validationMessages = array_merge_recursive($validateProduct->messages()->toArray(), $validationMessages); return redirect()->back()->withErrors($validationMessages)->withInput(); } // sure execute success, if not success rollback DB::transaction(function () use($request) { $user = $request->user(); // insert Product $product = new Product(); $product->key = Common::createKeyURL($request->input('Product.title')); $product->title = $request->input('Product.title'); $product->product_type_id = $request->input('Product.product_type_id'); $product->province_id = $request->input('Product.province_id'); $product->district_id = $request->input('Product.district_id'); $product->ward_id = $request->input('Product.ward_id'); $product->street_id = $request->input('Product.street_id'); $product->project_id = $request->input('Product.project_id'); $product->price_range_id = $request->input('Product.price_range_id'); $product->area_range_id = $request->input('Product.area_range_id'); $product->incense_type_id = $request->input('Product.incense_type_id'); $product->area = $request->input('Product.area'); $product->price = $request->input('Product.price'); $product->price_type_id = $request->input('Product.price_type_id'); $product->total_price = $request->input('Product.total_price'); $product->address = $request->input('Product.address'); if ($request->input('Product.expire_at') != null && $request->input('Product.expire_at') != '') { $expire_at = Carbon::createFromFormat('d/m/Y', $request->input('Product.expire_at'))->toDateString(); $product->expire_at = $expire_at; } $product->summary = $request->input('Product.summary'); $product->description = $request->input('Product.description'); $product->home_direction = $request->input('Product.home_direction'); $product->rooms = $request->input('Product.rooms'); $product->toilets = $request->input('Product.toilets'); $product->interior = $request->input('Product.interior'); $product->main_image = $request->input('Product.main_image'); $product->br_name = $request->input('Product.br_name'); $product->br_address = $request->input('Product.br_address'); $product->br_phone = $request->input('Product.br_phone'); $product->br_email = $request->input('Product.br_email'); $product->map_latitude = $request->input('Product.map_latitude'); $product->map_longitude = $request->input('Product.map_longitude'); $product->meta_description = $request->input('Product.meta_description'); $product->meta_keywords = $request->input('Product.meta_keywords'); $product->priority = $request->input('Product.priority'); $product->active = $request->input('Product.active'); $product->user_id = $user->id; $product->created_by = $user->name; $product->updated_by = $user->name; $product->save(); // save attachments if ($request->input('Product.attachments') != "") { $requestAttachments = explode(',', $request->input('Product.attachments')); $attachments = []; foreach ($requestAttachments as $key => $value) { if ($key == 0 && !isset($product->main_image)) { $product->main_image = $value; } array_push($attachments, new Attachment(['entry_id' => $product->id, 'table_name' => 'products', 'path' => $value, 'priority' => 0, 'is_publish' => 1])); } if (count($attachments) > 0) { $product->attachments()->saveMany($attachments); } } $product->save(); }); return redirect()->route('admin.products.index'); }