PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, April 18, 2022

[FIXED] Why can't I use latest() in my Eloquent code?

 April 18, 2022     eloquent, laravel, php     No comments   

Issue

I have been getting an error 'Method Illuminate\Database\Eloquent\Collection::latest does not exist' on this line of my code

     $items=Item::select('*')
    ->orderBy('version','DESC')
    ->get()
    ->unique('name')->latest()->paginate(5);

I'm not really sure why and how should I change my code?


Solution

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);



Answered By - Khamyl
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing