This video will explain Jeffrey discusses the n + 1 Problem https://laracasts.com/series/laravel-8-from-scratch/episodes/26
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();

Every single line of your code is totally wrong.
Definitely watch this video
https://www.youtube.com/watch?v=MF0jFKvS4SI&ab_channel=AdamWathan
and this series on Laracasts
https://laracasts.com/series/whip-monstrous-code-into-shape
and of course
Please or to participate in this conversation.