D
D
Dmitry2019-05-30 15:24:55
Laravel
Dmitry, 2019-05-30 15:24:55

How to check if unique value exists in database before creating new product in Laravel?

Hello.
There is

ProductssController.php

wherein
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());*/
        }
    }

The slug field in the database is unique
If there is a product with slug=="slug" in the database, I get an error
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'slug' for key 'products_slug_unique' (SQL: insert into `products` (`title`, `slug`, `exce

Tell me how can I remove this error and why it didn't work?
return back()->with('error', 'Product not added. Please try again.');

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danbka, 2019-05-30
@Danbka

https://laravel.com/docs/5.8/validation#rule-unique

A
Andrew, 2019-05-31
@manzadey

Add an additional check for uniqueness to the slug role check: unique:table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question