I have the following problem - I see, when I use the paginate() method on my eloquent models, Laravel makes 2 queries - a count query and select one.
My goal is to get the min and max values in table for a specific column. Is there a way to add select min(),max() in the count query or I have to make a separate one.
I'm talking about the laravel paginate() method.So for example
$products_q = Product::where('active',1); // sets the where
$products = $products_q->paginate(20); // this line here makes 2 requests:
# SELECT COUNT(*) FROM products WHERE active=1
#and
#SELECT * FROM products WHERE active=1 LIMIT 0,20;
My question is can I add somehow Min and Max in the first query with the paginate() method in laravel so the first query becomes like this :
SELECT COUNT(*),MIN(price) as min_price, MAX(price) as max_price FROM products WHERE active=1
This will aggregate the whole table result and give me only 1 row. I want aggregated results as well as the listing. Please read the question. It's specific with the paginate() method
why are you sweating it? Just write an extra query and cache the result so that you dont need to do it on page2,3 etc
How would you do it if you didnt use pagination? You would need an extra query because what you want is an aggregation and that cannot be in the same query as pulling the table