How to get last inserted id in laravel?

by mallory_cormier , in category: PHP Frameworks , 7 months ago

How to get last inserted id in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 7 months ago

@mallory_cormier 

In Laravel, you can use the insertGetId method to insert a record into a database table and retrieve the last inserted ID. Here is an example of how to use it:

1
2
3
$id = DB::table('your_table_name')->insertGetId(
    ['column1' => 'value1', 'column2' => 'value2']
);


This method will insert a new record into the specified table with the given column values and return the ID of the inserted record.