Have you tried the aggregates, in particular, min ?
barjas said:
yes, tried that, it returned single integer only
What did you try?
Anyways, just to save some time, here's the SQL that will get you in the right direction:
SELECT *
FROM books a WHERE price = (
SELECT MIN(price)
FROM books b
WHERE b.book_category_id = a.book_category_id)
A join will also work:
SELECT *
FROM books a INNER JOIN
(
SELECT MIN(price) as lowest
FROM books
GROUP BY book_category_id
) b ON a.price = b.lowest
But this is less of a laravel question than it is an SQL question. You could probably find it using "SQL lowest column in categories" ... If you can understand how the SQL works, then you could probably work it back out in the QueryBuilder or Eloquent.
I'm not a Laravel master, but if you ask me ... sometimes it's easier for me to plunk out the more complicated stuff in SQL than to play around with ORMs to get it right.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community