E
E
EVOSandru62020-07-20 20:39:58
Laravel
EVOSandru6, 2020-07-20 20:39:58

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();


2) Next, I pull out all the attributes associated with these services

$parentAttributes = Attribute::whereHas('services', function ($q) use ($ids) {
    $q->whereIn('services.id', $ids);
})->get();


But I need to get it as in example 0) and somehow insert the attribute_category_id field into the eloquent request and get the Attributes from the descendant to the ancestor. If the child has an attribute with this attribute_category_id , then do not search for the child.

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