Reputation: 128
I've a question about a relationship which involves one table connecting to two other tables with a many-to-many relation. I don't know if I need polymorphism or if I can just connect the table with the other as it is done in the laravel docs. I've joined an image below to show you what I exactly mean. From:
That would mean that one voucher could be used on multiple services and/or for buying stuff.
Let's say the voucher is worth a 500$ hairdresser. He could for example get his haircut (service) and buy hair care products (sale) with one the same voucher. Would my "To" image work? Do not mind the naming of the tables, etc. I've just created them for my example.
Thanks for reading
Upvotes: 1
Views: 350
Reputation: 344
Your "to" table will work.
In this case, a client can use the same voucher to a service AND/OR a sale. When you pull the vouchers used in a sale, this will get the relationships from the linkingtable_salevoucher
and when you pull the relationships from the services, this will use the linkingtable_servicevoucher
.
If you want the voucher can be used only service OR sale, not in both. Before you create the new relationship, you can check if has the other relationship.
Like this:
//Before create a Sale with voucher, check if voucher has be used
$voucher = App\Voucher::where('serialNumberOfProduct', $serialVoucher)
->has('services')->get();
if($voucher->count() == 0){
//create the new relationship
}
Hope this helps
Upvotes: 2