A
A
Alexander Pankov2021-06-21 15:15:24
PHP
Alexander Pankov, 2021-06-21 15:15:24

How to reduce the number of requests?

Hello, tell me how to properly organize the code and reduce the number of requests?

I have a certain model, let it be called "Model 1"
there is still a model, let it be called "Model 2" and it is a child of "Model 1"
there is still a model, let it be called "Model 3" and it is a child of "Model 2" "

Or for the subject area
- Model "Agreement 1"
- Model "Node 2"
- Model "Equipment 3"
- Model "Node 2"
- Model "Other Equipment 3"

an approximate such structure, nesting can be 3-4 levels
All these models are linked by hasOne\hasMany\BelongsTo... relays.


this is how the resource for the Contract is displayed (top level)

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        ..........
        'unit' => UnitResource::collection($this->units), // тут выводим все узлы догоовра
    ];
}


this is how the resource for the Node (2nd level) is displayed
public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        ..........
        'unit' => EquipmentResource::collection($this->units), // тут выводим все оборудование для Узла
    ];
}


everything seems to be ok, everything is fine, although it can be improved?
those if we display 10 contracts each of which has 10 nodes inside of which 10 equipment
is already heavy, I read about greedy loading with whenLoaded (cool), but have not tested it yet, I think that I use the wrong approach at the root

The essence of the problem is not really in This is the introductory part
. The essence:
Here we have Contracts, contracts have Accounting Nodes, and each accounting node has some kind of equipment

and business logic creates requests for changing this data, that is, an application
for changing a node or equipment in a node, and so on.
have statuses "in progress" \ "completed"
category and the data itself (application body) in json
table format: Applications, Application Attributes

and so
1) we have a route that displays aggregated information, they display one contract and all its nodes with equipment, 100 equipment can be found for such one contract (100 records in the database)
2) The task was to show how many active orders we we have now on the selected contract, node, equipment, those I need to knock on the database and filter the applications (count them at least ->count())

My question is that I don’t know where and how to do it right?

Now I have 3 API Resources
Agreement - Node - Equipment

When I pull to display the Agreement for me, it goes from there through ***Resource::collection(Model::relation)
to the very bottom and displays everything, they process all 3 php files to generate an array

now I decided on the forehead and I make additional requests in EVERY of the 3 files
Example
1) we pull aggregate information on the contract, return ContractResource::make($contact) is called
2) we get inside ContractResource.php
3) inside it I make 2 requests
3.1 ) to receive active applications of the required type (I get the id of applications suitable for search (roughly speaking, category + some other filters))
3.2) among these applications, I look for only those that relate to the current Agreement (JSON filter)
(the ones I have a table APPLICATION with general data for each type of application (1 request) and a table ATTRIBUTES_APPLICATIONS, which already contains JSON data on the application (2nd request))
4) I make these 2 requests, I get the data and attach them to the output
5) then through the relay I fall into a new resource (I display the node of this contract)
5) we get inside UntiResource.php
6) inside it I make 2 requests (and the first request is EXACTLY THE SAME AS ABOVE, those applications are in the same category)
6.1) to receive active claims of the required type (I get the id of claims suitable for search (roughly speaking category))
6.2) among these claims I look for only those related to the current Node (JSON filter)
7) I make these 2 requests, I get the data and attach them to the conclusion
8) then through the relay I fall into a new resource (I remove the equipment of this node)
9) we get inside EquipmentResource.php
10) inside it I make 2 requests (and the first request is EXACTLY THE SAME AS ABOVE, those applications are in the same category )
10.1) to receive active requests of the required type (I get the id of requests suitable for search (roughly speaking category))
10.2) among these requests I look for only those related to the current Node (JSON filter)

It seems to me that I have a terrible overhead
and make a request inside the toArray method of the resource class - not true at all,

tell me how to organize such an architecture correctly so that there are no extra / duplicate requests?

Total: there are 3-4 models, each refers to the next, we display them on one page and in other tables we also have data that needs to be found and attached at each of the levels

. Here is such a game
60d08529bff78784341847.png

base
60d087d778222962545570.png
60d087e0c50a4148064582.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daria Motorina, 2021-06-21
@glaphire

Perhaps the best solution would be to use raw requests with a bunch of joins, they are easier to control and less overhead

J
jazzus, 2021-06-22
@jazzus


so that there are no extra / duplicate requests?

Nested with and withCount and queries within them.

read about eager loading with whenLoaded (cool

whenloaded is not just a check.
resources work with loaded data. if you make requests to the database from them, as in the example, you will receive a request for each element of the collection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question