how to get last inserted record in laravel?

in Laravel Devloper how to get last inserted record in laravel?, I may need to get single last inserted record of Database table in our Popular PHP frameworks Laravel Web Application , I can get lots of the way laravel eloquent join with get current inserted Id. I can Get the last record of MySQL database table using inbuilt function like as a latest() or orderBy(). In following th example you can watch how i get or fetch after that last rows of DB table.

I have “products” DB table as well as i want to fetch the last row or DB record of “products” table. In simple example i use the Laravel function latest() with first() so latest() will fetch single latest row according to created_at after that first() will get single record:

Read Also:  Remove empty values from array in PHP
how to get last inserted record id in laravel, how to get last inserted record in laravel, get last inserted record id in laravel
get last inserted record in laravel
  • $insertgetid_last = DB::table(‘bolly4u’)->latest()->first();
  • $insertgetid_last_example2 = DB::table(‘tamilrockers’)->orderBy(‘id’, ‘DESC’)->first();
  • $insertgetid_last_example_3 = DB::table(‘khatrimaza’)->latest(‘id’)->first();

Using latest() belong to created_at field:

$get_last_record = DB::table('products')->latest()->first();
dd($get_last_record);

And then, In this below example We used to simple inbuilt methods of orderBy() that belong to database last id, it will always DB Query return max id row:

Using Select Last Row in the Table

For Pre-Laravel 4

return DB::table('products')->order_by('upload_time', 'desc')->first();

For Laravel 4 and onwards

return DB::table('products')->orderBy('upload_time', 'desc')->first();

For Laravel 5.7 and onwards

return DB::table('products')->latest('upload_time')->first();

Use the latest scope

Product::latest()->first();

To get last record details

Product::all()->last();
//or
Product::orderBy('id', 'desc')->first();

To get last record id

Product::all()->last()->id; or
Product::orderBy('id', 'desc')->first()->id;

Using orderBy() belong to id field:

$get_last_record = DB::table('products')->orderBy('id', 'DESC')->first();
dd($get_last_record);

In this Laravel 6 example using latest() with Parameter, latest() will take your DB Date Table Collumn created_at by default, if you want to give your Table id then you can give Parameter as any field name.

Read Also:  Laravel 5/6/7 Carbon date format Example

Using latest() belong to id field:

$get_last_record = DB::table('products')->latest('id')->first();
dd($get_last_record);

But If you want to fetch last inserted id in Laravel Latest Verion then you have to follow this link :

How to get last inserted id in laravel 5 and 6?.

insertgetid laravel eloquent

In PHP, you use simple function like as mysqli_insert_id to get last inserted record id in MySQL Database Table.

Solution: how to get last inserted record in laravel 5/6/7?
In laravel 5/6/7, when you go with DB some query builder then you can use insertGetId() that will CRUD insert or Update a record as well as then return last Database inserted row id.

Read Also:  vue Check box binding Example
how to get last inserted record id in laravel, how to get last inserted record in laravel, get last inserted record id in laravel,get last inserted id in laravel, how to get last inserted id in laravel, get last inserted id laravel, get last inserted id laravel query builder, laravel get last inserted id after save, laravel get last inserted id
how to get last inserted record in laravel

Way 1:

public function getLastInsertedId()
{
$id = DB::table('products')->insertGetId(
['pcode' => 'p9825656', 'name' => 'Mobile']
);

dd($id);
}

Way 2:

public function getLastInsertedId()
{
$product = Product::create(['name'=>'Mobile' , 'pcode'=>'p9825656']);

dd($product->id);
}

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to get last inserted record in laravel?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  nested select sql
  2. Read Also:  Laravel 5.6 PDF Generation using Dompdf Example
  3. Read Also:  Enable/Disable debug mode in cakephp
  4. Read Also:  select query in php mysql with example
  5. Read Also:  GROUP_CONCAT in laravel Eloquent/Raw
  6. Read Also:  Merge Multiple PHP JSON Array Merge into one array
  7. Read Also:  Laravel Number of days between two inputted dates
  8. Read Also:  PHP Rest API Framework List with Features
  9. Read Also:  Laravel Carbon format Live datetime Example
  10. Read Also:  Laravel 6 Joins Example Tutorial