amit sutar
amit sutar

Reputation: 541

How to Get Last Updated Record with Laravel Query Builder?

I'm using Laravel Query Builder to get the last updated record from the database. However, it displays a different id with a different update_date. The update_date is correct, and it shows me last updated_date, but every time I'm getting the first id.

$results = DB::table('customer_info as cust_info')
    ->join('client_emp as client_info', 'cust_info.id', '=', 'client_info.cust_id')
    ->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))
    ->orderBy('cust_info.id','DESC')
    ->first();

In the above query, I write select to get the id of last updated record.

->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))

However, it shows me the last updated_date only with the first id from the table every time.

Upvotes: 0

Views: 6778

Answers (1)

Piazzi
Piazzi

Reputation: 2636

Try this:

orderBy('updated_at','DESC')->first()->id

Here you are getting the id of the latest updated record. When you remove the first() method, you will get the whole collection sorted by 'updated_at'

Upvotes: 3

Related Questions