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?
To do it in raw SQL, use:
Product::query()->selectRaw('MIN(width) as min_width, MAX(width) as max_width, MIN(height) as min_height, MAX(height) as max_height, MIN(depth) as min_depth, MAX(depth) as max_depth')->get();