public function add(Request $request) { $category = Category::all(); if ($request->isMethod('post')) { $input = $request->all(); $fileName = null; if (!empty($input['image_url'])) { $file = $input['image_url']; $extension = $file->getClientOriginalExtension(); $fileName = rand(11111, 99999) . '.' . $extension; $file->move("admin/product", $fileName); $destinationPath = "admin/product"; $fileThumb = "400x300_" . $fileName; $fileThumbBig = "600x500_" . $fileName; $img = Image::make("{$destinationPath}/{$fileName}")->resize(400, 300); $img->save("{$destinationPath}/{$fileThumb}"); $imgBig = Image::make("{$destinationPath}/{$fileName}")->resize(600, 500); $imgBig->save("{$destinationPath}/{$fileThumbBig}"); } $product = new Product(); $product->price = $input['price']; $product->code = $input['code']; $product->categories_id = $input['category']; $product->image_url = $fileName; $product->slug = $this->slugify($input['title']); $product->translateOrNew(LaravelLocalization::setLocale())->title = $input['title']; $product->translateOrNew(LaravelLocalization::setLocale())->summary = $input['summary']; $product->translateOrNew(LaravelLocalization::setLocale())->description = $input['description']; $product->save(); return redirect(LaravelLocalization::setLocale() . DIRECTORY_SEPARATOR . 'admin-product'); } return view('admin.products.add', compact('category')); }
/** * 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'); }