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

koenverhulst's avatar

Eloquent: belongsToMany with union

Hi,

consider the following simplified datamodel:

A product can contain many options. An option belongs to many products.

3 tables: Products, Options and Product_Options

However, there is an attribute "linked_with_all_products" (true/false) on the Options table, when checked, the option is linked with all products and no records are inserted in Product_Options.

Simple question: what is the correct way to retrieve all the options linked with a certain product?

(I'm new to eloquent and laravel)

Thanks in advance,

Koen

0 likes
3 replies
jlrdw's avatar

With a union you will probably be better off using the query Builder.

koenverhulst's avatar

Thanks for your reply jlrdw!

Isn't there an easy way to add records to an existing relationship?

Kind regards,

Koen

Please or to participate in this conversation.