2

How to get latest updated or created record in laravel

if i use latest() it only considered created_by value.

$tasks->latest();

if i use orderBy

$tasks->orderBy('created_at,'DESC');

then it is not working for latest updated.

$tasks->orderBy('updated_at,'DESC');

then its is not working for created_at

How to get latest created or latest updated Record. Basically, the most recent activity record? Thanks in advance!

Neha
  • 2,136
  • 5
  • 21
  • 50
  • 1
    Possible duplicate of [Laravel - get last row using whereHas](https://stackoverflow.com/questions/48276324/laravel-get-last-row-using-wherehas) – Pathik Vejani Aug 01 '19 at 05:50
  • 1
    just order by `updated_at` field, this will fetch the most recent activity record – Sohel0415 Aug 01 '19 at 05:52
  • 1
    `$tasks->latest('your_column');`.. also you can use multiple orderBy in chain..`$tasks->orderBy('created_at,'DESC')->orderBy('updated_at,'DESC');` – danish-khan-I Aug 01 '19 at 05:52

1 Answers1

1
$tasks->orderBy('updated_at,'DESC');

updated_at is the most recent timestamp on any activity (created/updated), so this should work in both the cases.

Zeshan
  • 2,496
  • 3
  • 21
  • 26