How to get the id of inserted row in laravel?

by muriel.schmidt , in category: PHP Frameworks , 5 months ago

How to get the id of inserted row in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by lindsey.homenick , 5 months ago

@muriel.schmidt 

In Laravel, you can get the id of the inserted row using the insertGetId() method when inserting data into the database. Here's an example:

1
2
3
4
5
$id = DB::table('users')->insertGetId(
    ['name' => 'John Doe', 'email' => '[email protected]']
);

echo $id;


This code will insert a new row into the users table with the specified data and will return the id of the inserted row. You can then use this $id variable as needed in your application.