M
M
muhasa2018-04-07 15:06:41
Laravel
muhasa, 2018-04-07 15:06:41

How to link a model to itself in Eloquent?

Hello.
I decided to implement the "Similar Products" functionality in a self-written store.
We will put products in the admin panel for each product.
All this is written in a table.

product_related
product_id | product_related_id

And how to connect the table to itself in the Eloquent model, I roughly understand.
But here's the question - is everything right from an architectural point of view?
Here is the situation. We put the first product in line with the second
product_related
product_id | product_related_id
1                  2

And what should we now insert a record below
product_related
product_id | product_related_id
2                  1

Which will be a complete repetition of the first option.
How should such schemes be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-07
@Yan-s

It depends on how you are going to interpret such records.
For example, if product A page displays product B as related, this does not necessarily mean that product A should be displayed as related on product B page.
Otherwise, the presence of a duplicate relationship must be controlled at the application level by making a verification request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question