[英]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.