Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Johny22's avatar

Optimization query

Hi, in the application I use queries which relate data from the database. Everything still works, but I would like to optimize those queries because when loading the page, 16 queries and 14 duplicates light up in my debugbar. Could it help do the scope in this case? If so, could you tell me what such a scope should look like? If not, how could I solve it? Thank you very much.


// getDimensions

 $dimensions = Country::where('country_code', $request->selectedCountry)
            ->first()->categories()->where('max_dimension', '>=', $request->dimension)
            ->orderBy('max_dimension', 'asc')->get();

// store
$countries = Country::with([
            'services' => function ($query) use ($request) {
                $query->whereIn('services.id', Arr::wrap($request->services));
            },
            'categories' => $closure = function ($query) use ($request) {
                return $query->where('categories.id', $request->category_id);
            }
        ])->whereHas('categories', $closure)
            ->where('country_code', $request->delivery_country)
            ->first();

GitHub Logo

0 likes
13 replies
MichalOravec's avatar

Show whole ShipmentController and what is on lines 53, 88, 92.

Johny22's avatar
$countries = Country::with(['categories', 'services'])->get(); //53

$categories = Country::with(['categories', 'services'])
            ->where(['country_code' => $countryCode])->first()->categories()->get(); //88

$countries = Country::with(['services'])->whereHas('categories')
            ->where('country_code', $countryCode)
            ->first(); //92

MichalOravec's avatar

It doesn't make any sense.

Why you have there 2 times $countries?

For example this piece of code

// it generates 4 queries

$categories = Country::with(['categories', 'services'])
            ->where(['country_code' => $countryCode])->first()->categories()->get(); 

should be

// here it will be only one query

$categories = Category::whereHas('countries', function ($query) use ($countryCode) {
    $query->where('country_code', $countryCode);
})->get();
Johny22's avatar

I have to delete the controller, it's a shame I know :(

MichalOravec's avatar

No don't do that.

Su tu vacsi hromotlci, toho sa bat nemusis, len studuj zaplat si laracasts alebo tutorialy od Yablka na learn2code a da ti to mnoho.

1 like
Johny22's avatar

skusim to refactornúť... aspoň som v obraze

MichalOravec's avatar

Len taky doplnok, pomlcka v nicku na Laracasts neni moc dobre mat, lebo potom sa ta neda oznacovat.

Johny22's avatar

Hi please I'm trying to optimize the queries as you advised me but I can't get to the relations pivot country_category. Please advise me how to get to that pivot

// it generates 4 queries

$categories = Country::with(['categories', 'services'])
            ->where(['country_code' => $countryCode])->first()->categories()->get(); 


// here it will be only one query

$categories = Category::whereHas('countries', function ($query) use ($countryCode) {
    $query->where('country_code', $countryCode);
})->get(); //get pivot

Please or to participate in this conversation.