Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: get last Insert id from query builder

Tags:

laravel-5

I am new in laravel and trying to get last inserted id from this query:

DB::select("INSERT INTO current_survey (`name`, `created_by`, `description`)  SELECT `name`, `created_by`, `description` FROM survey WHERE id=survey_id");

this query working well, can any one suggest me how this is possible in laravel?

Thanks

like image 344
Paljinder Dhillon Avatar asked May 16 '17 11:05

Paljinder Dhillon


People also ask

What is last insert ID?

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

What are the difference between Insert () and insertGetId () in laravel?

Insert(): This function is simply used to insert a record into the database. It not necessary that ID should be autoincremented. InsertGetId(): This function also inserts a record into the table, but it is used when the ID field is auto-increment.


Video Answer


1 Answers

Try it once :-

$id = DB::getPdo()->lastInsertId();
like image 168
kunal Avatar answered Sep 30 '22 06:09

kunal