Support the ongoing development of Laravel.io →
Database Laravel.io
Last updated 2 years ago.
0

Have you tried the aggregates, in particular, min ?

https://laravel.com/docs/5.4/queries#aggregates

0

yes, tried that, it returned single integer only

0

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.

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

barjas barjas Joined 27 Feb 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.