Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nogepog's avatar

groupby table values in eloquent

I have three rows day_id|stationary_id|amount fk fk number how do i query eloquent to group by columns and find on which day which stationary was issued and in what amount

e.g

1 | 1 | 3

1 | 4 | 5

2 | 4 | 1

2 | 1 | 2

how do i determine If a stationary item is issued 2 or more days

0 likes
2 replies
jlrdw's avatar

Not knowing your exact set up, here is what a basic group by might look like:

$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
                ->select('dc_powners.ownerid', 'dc_powners.oname')
                ->selectRaw('count(dc_pets.petid) as countOfPets')
                ->groupby('dc_powners.ownerid')
                ->orderby('dc_powners.oname')
                ->get();

Results basically give:

ownerid, oname, countOfPets

Like:

5|Bob|3
4|Greg|9
2|Rob|1

https://laravel.com/docs/8.x/queries

2 likes

Please or to participate in this conversation.