Get last insert id after insert query laravel framework
In this example i am going to show you how to get the last insert id after insert data in database.
public function getLastInsertedId()
{
$id = DB::table('users')->insertGetId(
['email' => 'divya@gmail.com', 'name' => ‘Divya Sundar']
);
print_r($id);
}
Example 2
public function getLastInsertedId()
{
$user = User::create([‘name'=>'Divya Sundar','email'=>'divyasundarsahu@gmail.com']);
print_r($user->id);
}
insert_id() function will return zero if table does not have auto incremented column so make sure table must have a column with AUTO_INCREMENT attribute.