S
S
sr362019-12-16 18:08:48
Laravel
sr36, 2019-12-16 18:08:48

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

Laravel debug bar shows 720 requests on main page, how can I optimize this page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2019-12-16
@sr36

Section::with(['categories' => function ($cat) {
$cat->withCount('offers');
}])->get()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question