0

I have an array of model entities, each one has a used_at carbon timestamp, I was wondering how can I retrieve the last model to be used (latest used_at) timestamp of the record of models.

$models = Model::all();

How can I get the last used model with Eloquent?

gabogabans
  • 3,035
  • 5
  • 33
  • 81
  • 1
    May be `$models = Model::latest('used_at')->get();` for model instance only the last record `$models = Model::latest('used_at')->first();` or ` `$models = Model::orderBy('used_at', 'DESC')->first();` – STA Oct 28 '20 at 14:42
  • Does this answer your question? [Laravel Eloquent Relations: ->latest()](https://stackoverflow.com/questions/36755804/laravel-eloquent-relations-latest) – Tim Lewis Oct 28 '20 at 14:45

1 Answers1

0
Model::latest('used_at')->first();

https://laravel.com/docs/8.x/queries#latest-oldest

Naxon
  • 1,354
  • 4
  • 20
  • 40