If you want to get the last record inserted into table in laravel application then here we are gives you very easy and best laravel methods which is get your latest inserted record from the table.
Basically we want the last record but its latest record behalf the mysql database. So don’t worry for how to get the last insert data in laravel project here we can use two methods to retrieve last record (latest) in easy way. The latest with first and orderBy id to get the last insert id in laravel is very example.
Example 1: Using latest method
Here we have a posts table and we get the last record from the post table using latest() with first() methods. The latest latest() will fetch only latest record according to created_at and the first() will get only single record.
$lastRecord = Post::latest()->first();
Example 2: Using Order By
$lastRecord = Post::orderBy('id', 'DESC')->first();