I'm trying to improve the performance of my laravel application. I was already able to reduce the amount of queries from 68 to 20 by removing lazy loading in my views.
However, using eager loading, still 20 queries remain that do almost the same thing. My code looks like this:
$products = [
'latest' => Product::with('vehicle', 'brand', 'type', 'photos')->withoutGlobalScope(ProductScope::class)->latest()->take(5)->get(),
'most_viewed' => Product::with('vehicle', 'brand', 'type', 'photos')->withoutGlobalScope(ProductScope::class)->mostViewed()->take(5)->get(),
'nearest' => Product::with('vehicle', 'brand', 'type', 'photos')->withoutGlobalScope(ProductScope::class)->nearest($address)->take(5)->get(),
];
This results in 15 queries (5 each) since every time the relations will also be queried again. Can these queries be combined in such a way that it might be reduced to 7 queries instead of 15?