Answer the question
In order to leave comments, you need to log in
How to check if unique value exists in database before creating new product in Laravel?
Hello.
There is
ProductssController.php
public function addProduct(){
$objCategories = new Categories();
$categories = $objCategories->get();
return view('admin.products.products.add', ['categories' => $categories]);
}
public function addRequestProduct(Request $request){
try{
$this->validate($request, [
'title' => 'required|string|min:4|max:25',
'slug' => 'required|string|min:4|max:25',
'excerpt' => 'required|string|min:4|max:25',
'content' => 'required|string|min:4|max:300',
'descrtitle' => 'required|string|min:4|max:25',
'descrtext' => 'required|string|min:4|max:25',
'descr' => 'required|string|min:4|max:300',
'regular_price' => 'required|regex:/\d+/',
'discount' => 'required|regex:/^\d+(\.\d{1,1})?$/',
'currency' => 'required|string|min:4|max:25',
'image' => 'mimes:jpeg,jpg,png,gif|max:10000',
'tab_bg' => 'mimes:jpeg,jpg,png,gif|max:10000',
]);
$is_reccomended = $request->has('is_reccomended') ? true : false;
$image = ImageDNK::save($request, 'image');
$tab_bg = ImageDNK::save($request, 'tab_bg');
$objProduct = new Products();
$objProduct = $objProduct->create($request->all(), [
'is_reccomended' => $is_reccomended,
'image' => $image,
'tab_bg' => $tab_bg,
]);
$objCatsRels = new CategoriesRelationship();
$objCatsRels = $objCatsRels->create([
'object_id' => $objProduct->id,
'category_id' => $request->product_category,
]);
if($objProduct && $objCatsRels){
return redirect(route('admin.products.edit', ['id' => $objProduct->id]))->with('success', 'Товар успешно добавлен');
}
return back()->with('error', 'Товар не добавлен. Попробуйте ещё раз.');
}catch(ValidationException $exception){
\Log::error($exception->getMessage());
return back()->with('error', $exception->getMessage());*/
}
}
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'slug' for key 'products_slug_unique' (SQL: insert into `products` (`title`, `slug`, `exce
return back()->with('error', 'Product not added. Please try again.');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question