Laravel: How to recover last entry in mysql database by your foreign key?

Asked

Viewed 1,781 times

1

I am working on a database that has only as a reference to your foreign key, and I need to locate the last record according to your foreign key.

1 answer

3


If you have the standard timestamp fields of the Laravel, you can give a

SuaClasse::where('chave_estrangeira', $chave)->orderBy('created_at', 'desc')->first();

Otherwise an option would be to orderby the ID in the same way, comparing the foreign key

  • Thank you Ademilson Santana da Silva saved my day!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.