How to get the id of inserted row in laravel?

by muriel.schmidt , in category: PHP Frameworks , a year ago

How to get the id of inserted row in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by lindsey.homenick , a year 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' => 'john.doe@example.com']
);

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.

Related Threads:

How to get last inserted id in laravel?
How to get last inserted row in cakephp?
How to get/return last inserted id in codeigniter?
How to get row number of a row in laravel?
How to get id after insert in Laravel?
How to get the id of current saved model in laravel?