Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

calin.ionut's avatar

how to validate uniqueness for 2 columns?

I am having some issues validating 2 columns that i want them to be unique.

A am having 3 tables: products, attributes and products_attributes.

I want the product to have unique attributes assigned. I don`t want to use any rellations between those tables (hasMany, etc....)

The products_attributes model looks look like this:

Schema::create('products_attributes', function (Blueprint $table) {
            $table->increments('ProductAttributeID'); // the primary key
            $table->integer('AttributeID')->unsigned(); // primary key for attributes table
            $table->integer('ProductID')->unsigned(); // primary key for products table

            $table->integer('Stock');
            $table->string('SKU')->unique();
            $table->decimal('Price', 10,2);
            $table->timestamps();

            $table->unique(['ProductID', 'AttributeID']);
        });

Inserting and updating the data I use the id for this table: ProductAttributeID => when the value is 0 I am adding new data else update the existing data.

The validation and saving the data is this:

// update or save product attribute ?
        if(isset($request->ProductAttributeID) && $request->ProductAttributeID != 0) {
            $productAttribute = AttributeProduct::findOrFail($request->ProductAttributeID);
            $validator = Validator::make($request->all(), [
                'AttributeSKU' => [
                    'required', 'string', 'max:255', Rule::unique('products_attributes', 'SKU')->ignore($productAttribute->ProductAttributeID, 'ProductAttributeID')
                ]
            ]);
            if ($validator->fails()) {
                return response()->json(['error' => true, 'msg' => 'U: The SKU field is required and must be unique!']);
            }            
        } else {
            $productAttribute = new AttributeProduct();
            $validator = Validator::make($request->all(), [
                'AttributeSKU' => 'required|unique:products_attributes,SKU|string|max:255'
            ]);
            if ($validator->fails()) {
                return response()->json(['error' => true, 'msg' => 'N: he SKU field is required and must be unique!']);
            }            
        }

        $validator = Validator::make($request->all(), [            
            'AttributeName' => 'required|numeric',
            'AttributePrice' => 'required|numeric',
            'AttributeStock' => 'required|numeric',
            'FKey' => 'required|numeric'
        ]);
        if($validator->fails()) {
            return response()->json(['error' => true, 'msg' => 'All the fields are required!']);
        }

        // make sure the SKU is unique through products table
        $codeExist = Product::where('Code', $request->AttributeSKU)->first();
        if($codeExist){ return response()->json(['error' => true, 'msg' => 'SKU  allready exist!']); }

        // save or update attribute product
        $productAttribute->AttributeID = $request->AttributeName;
        $productAttribute->ProductID = $request->FKey;
        $productAttribute->Stock = $request->AttributeStock;
        $productAttribute->SKU = $request->AttributeSKU;
        $productAttribute->Price = $request->AttributePrice;
        $productAttribute->save();

How can i validate the uniqueness for ['ProductID', 'AttributeID'] ???

0 likes
1 reply
calin.ionut's avatar

Old try and catch works.

try{
            $productAttribute->AttributeID = $request->AttributeName;
            $productAttribute->ProductID = $request->FKey;
            $productAttribute->Stock = $request->AttributeStock;
            $productAttribute->SKU = $request->AttributeSKU;
            $productAttribute->Price = $request->AttributePrice;
            $productAttribute->save();
        } catch (\Exception $e){
            return response()->json(['error' => true, 'msg' => 'The attribute is allready assigned to this product!']);
        }

Is there other way? :)

Please or to participate in this conversation.