Answer the question
In order to leave comments, you need to log in
How to optimize queries for Laravel?
Good evening.
Faced the problem of query optimization on Laravel.
Site - bulletin board.
The main page displays all categories and subcategories, plus the number of ads in each of them.
This is how the output goes
@foreach($sections as $section)
<div class="col-md-3 mb-3">
<div class="card">
<div class="card-header" style="padding: 3px 10px;"><h3 class="empty">{{$section->name}} </h3></div>
<div class="card-body">
@foreach($section->categories as $cat)
<a class="reverse" href="#">{{$cat->name}}</a> ({{$cat->offers->count()}}) <br>
@endforeach
</div>
</div>
</div>
@endforeach
Answer the question
In order to leave comments, you need to log in
Section::with(['categories' => function ($cat) {
$cat->withCount('offers');
}])->get()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question