G
G
Glory2021-06-11 23:04:05
Laravel
Glory, 2021-06-11 23:04:05

How to get hierarchical structure after executing a query?

This question is specific to Laravel.
The question is: How to get a hierarchical structure without a with construct after a data query?

Here is what I need to get:

Main product 1
- offer1
- offer2
- offer3
Main product 2
-offer4
-offer5
.....

This is what I get with the code: But it so happened that I can’t use the with construct, but I need to get a nested selection. how to achieve this? I have a very complex code. There, using Eloquent, we obtain the required request from two similar entities. then we convert it to SQL, combine queries using mergeBindings
Product::with('offers')->get();



and get the final query. there already construction with does not operate. After all, it's not Eloquent anymore.

So you need to understand how to use the query builder to get the opportunity to link one table to another and get not just a selection of records, but a selection taking into account the nested structure, as if it were with ()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2021-06-12
@Kostik_1993

At the database level, within the current query, nothing. Use the second query to select the products of the second level by the id of the top-level products and combine them at the array level.
It seems to me that the participants of the toaster write your project for you. You need to think with your head, otherwise your brain will become numb.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question