latest()
is a Builder class method not a Collection class method see: https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Builder.html#method_latest
So use it before getting the collection by get()
or paginate()
:
$items=Item::select('*')
->orderBy('version','DESC')
->latest()
->get()
->unique('name');
Using pagination on unique values is a bit more complicated. We would need to know what to select if there is more records with the same name. The most simple approach is to use distinct()
but this may not be what you need:
$items=Item::select('*')
->orderBy('version','DESC')
->latest()
->distinct()
->paginate(5);
Other possibility is to use groupBy('name')
instead of distinct()
, but there you must carefully aggregate the other columns around the 'name' (or select only the 'name' column). Example:
$items=Item::select('name', DB::raw('MAX(version) as max_version'))
->orderBy('max_version','DESC')
->group_by('name')
->paginate(5);
If you need more columns selected use appropriate aggregate functions MAX() or FIRST(), LAST(), AVG(), etc..
To get the whole row (simple '*' cannot be used):
$max_version_table = Item::select('model_name', DB::raw('MAX(version) as max_version'))->groupBy('model_name');
$items = Item::select('*')->joinSub($max_version_table, 'max_version_table', function($join){
$join->on('items.model_name','=','max_version_table.model_name');
$join->on('items.version','=','max_version_table.max_version');
})->paginate(5);