laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Group + order doesn't work

Open BenjaminOwlient opened this issue 7 years ago • 5 comments
trafficstars

Hi,

I try to get the oldest item in each group from a collection :

MyModel->orderBy('date', 'asc')->groupBy('color')->get(['color', 'date']);

It doesn't work : each group gets the last date of the collection, like if the orderBy wasn't taken into account.

Any idea on how to perform that ?

BenjaminOwlient avatar Feb 28 '18 11:02 BenjaminOwlient

what is ->group() ? is that a custom scope? Or a new feature? or do you mean groupBy?

vesper8 avatar Mar 05 '18 23:03 vesper8

Yes I mean groupBy, thank you I edited.

BenjaminOwlient avatar Mar 06 '18 09:03 BenjaminOwlient

MyModel->groupBy('color')->orderBy('date', 'asc')->get(['color', 'date']);

maybe this works

faghihi avatar Jul 22 '18 17:07 faghihi

@jenssegers is that possible... to use both ?

muzafarali avatar Nov 20 '18 14:11 muzafarali

@muzafarali Did you solve a problem?

andrii-trush avatar May 04 '23 21:05 andrii-trush