Reputation: 303
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.
Upvotes: 3
Views: 14766
Reputation: 2339
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.
Upvotes: 9
Reputation: 514
use insertGetId in place of insert
$id=DB::table('categories')->insertGetId($insert);
$id
is your last insert id
Upvotes: 1