Get the last inserted id using laravel
laravel-5, mysql, php
Solution
Instead of using
\DB::table('categories')->insert($insert);
Use
\DB::table('categories')->insertGetId($insert);
The method insertGetId will insert the datas and then return the incremented ID of the inserted line.
Problem
I am stuck with a problem . i need the last inserted id from the table which is to be added to same table, i.e in my table i 've two fields "id" and "translation-of" they both should save the same value. Id field is auto increment.How to implement this???? Am also uploading the categories using excel in that case also same problem... Please help me with this.... my controller function is ``` $insert[] = ['translation_lang'=>'en','parent_id' =>$a[0],'name' => $value->name,'slug' =>$value->name,'description' => $value->description,'picture'=>$pic,'active'=>1]; $last_id = \DB::table('categories')->max('id'); if(!empty($insert)){ \DB::table('categories')->insert($insert); \DB::table('categories')->where('id2',$last_id)->update(['translation_of' => $last_id]); return Redirect::to('admin/category'); } ``` Waiting for a response.