Sunday, April 16, 2017

Laravel multiple occurrences of relation

I'm building an api for a clothing store with Laravel 5.4 and I have this situation. I have a Rack that has clothes on it. But the same piece can occur multiple times on the rack.

rack
-id
-description
product
-id
-description
product_rack
-id
-rack_id
-product_id

I have a many to many relation because a product can occur on many racks and a rack has multiple products. However I'am not sure how I can attach a product multiple times to a rack.

Like for instance Rack with id of 1 has 2 times product 5 and 1 time product 3.

I can use the attach() function but if I detach() it releases all products from the rack instead of one.



via job vink

Advertisement