Answer the question
In order to leave comments, you need to log in
How to select attributes in laravel eloquent with removal of duplicates from descendant to ancestor?
Good afternoon
There are some announcements to which attributes are attached.
Attributes, in turn, are bound to services.
adverts (Advert model)
--
id
service_id
services (Service model) --
id
parent_id
_lft
_rgt
attributes
( Attribute
model
)
--
attribute_category_id
service_id
advert_id
name which service_id is maximum (link to service with maximum parent_id )
For example - there are services:
0)
- Special equipment ( attributes: weight (1), length (2), width (3), height (4) )
- - Daughter of special equipment (attributes: stability (5) , power (6))
- - - Grandson of special equipment (attributes: stability (7), height (8))
I.e. If I request attributes for an ad linked to the Grandson of Special Equipment service, I should get:
stability (7),
height (8),
power (6),
weight (1),
length (2),
width (3)
I tried to take it this way:
1) First, regarding the service_id of the ad, I get a list of descendant service IDs and add the current one
$ids = $this->ancestors()
->orderBy('_lft')
->pluck('id')
->merge($this->id)
->toArray();
$parentAttributes = Attribute::whereHas('services', function ($q) use ($ids) {
$q->whereIn('services.id', $ids);
})->get();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question