I have a query like $products = Product::query()->where(...)...
. I want to read the maximum dimensions for my double range sliders something like:
$dimensionLimits = [
'width' => [
'min' => $products->min('width'),
'max' => $products->max('width')
],
'height' => [
'min' => $products->min('height'),
'max' => $products->max('height')
],
'depth' => [
'min' => $products->min('depth'),
'max' => $products->max('depth')
],
];
My problem that this is 6 relative slow queries. Is there a way to optimize this into a single query? I know it is possible by adding raw SQL, but is there a way to do it with Eloquent generated queries?
2
Answers
To do it in raw SQL, use:
In this scenario you can do something like this to get both
pagination()
andaggregation
results.Step1 :- Fetch paginated records
Step 2:- Fetch aggregated result-set