Laravel: How to retrieve the last record inserted in the mysql database by its foreign key?

1

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

    
asked by anonymous 09.11.2018 / 19:53

1 answer

2

If you have the standard timestamp fields of laravel, you can give

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

Otherwise an option would be to give the orderBy by ID in the same way, comparing the foreign key

    
09.11.2018 / 20:00