Laravel: get latest insert id from query builder

I am new to laravel and am trying to get the last inserted id from this request:

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

      

this query works well, can anyone suggest me how this is possible in laravel?

thank

+3


source to share


2 answers


Try it once: -



$id = DB::getPdo()->lastInsertId();

      

+3


source


Try the following:



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

//your inserted id
$last_id = $query->id;

      

0


source







All Articles