[英]Get the last inserted id using laravel
我遇到了一個問題。 我需要從表中插入的最后一個 id 將被添加到同一個表中,即在我的表中我有兩個字段“id”和“translation-of”,它們都應該保存相同的值。 Id 字段是自動遞增的。如何實現???? 我也在使用excel上傳類別,在這種情況下也是同樣的問題......請幫我解決這個......
我的控制器功能是
$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');
}
等待回應。
而不是使用
\DB::table('categories')->insert($insert);
使用
\DB::table('categories')->insertGetId($insert);
方法insertGetId將插入數據,然后返回插入行的遞增 ID。
使用 insertGetId 代替 insert
$id=DB::table('categories')->insertGetId($insert);
$id
是您的最后一個插入 ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.