How to get last inserted id in Laravel?
In this post we will give you information about How to get last inserted id in Laravel?. Hear we will give you detail about How to get last inserted id in Laravel?And how to use it also give you demo for it if it is necessary.
Its very common need to get last insert id in programming field, if you are working on laravel framework and you want to fetch last created id, i mean max id, then you do easily.laravel provide method to get last inserted id.
If you are working with DB::table() then you have to use insertGetId() for insert data into database. this function always return inserted record id(primary key).it is very simple way to get id, in following example you can see and undestand more:
Example:
public function getLastInsertedIdDB()
{
$id = DB::table('items')->insertGetId(['name'=>'hello']);
print_r($id);
}
But, if you are insert record using laravel Model then it is pretty simple to get without using any special function like insertGetId(), in following example you can see and how to use i create method.
Example:
public function getLastInsertedIdModel()
{
$create = Item::create(['name'=>'hello']);
print_r($create->id);
}
Hope this code and post will helped you for implement How to get last inserted id in Laravel?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs