0

I have the following problem: I have a product table that has a cod column. This product table has a one to many relationship with variation, one product can have multiple variations. The variation table also has a cod column.

  product     variation2      here product has two variations      
   cod 12       cod 13
   id 1         product_id 1
   user_id 100

         variation1
         cod 14
          product_id 1

The user cannot insert the product if he already inserted a product or variation with a specific code.

I need to validate this using laravel. I really don't want to use eloquent to to this. I with I could use Laravel validation.

The farthest I could get was:

  "cod" => 'unique:product|unique:variation'

But how can I base the validation by user_id on the product table?

1 Answers1

0

For this kind of validation just make select for tables product variation1 and variation2 (with joins on 'cod') and if you will not get any row, then you can save model (still inside transaction). Because this situation is encapsulated to only one user (and I assume he can work from one computer at the same time) then you don't need to use transaction and optymistic/pesymistic locking

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345