D
D
Dmitry2021-01-19 10:35:38
Laravel
Dmitry, 2021-01-19 10:35:38

How to create such a relationship in Eloquent?

Hello!
I would appreciate a recommendation on Laravel Eloquent.
Suppose an online store catalog is created, which has a Product model (product) and an Option model (product feature). There is a need to arbitrarily create different characteristics with different attributes (for example, different Option may have different units of measure, etc.). At the output, you need the ability to get the value of any option for any model:

$product = Product::find(1);
$product->option;

Abstracting from Eloquent, I would just make the following tables:
products (id,...)
options (id,...)
products_options(product_id,option_id,option_value)

Is it possible to implement this through relationships in Eloquent, and if so, how?
Thanks in advance for your advice.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question