使用 laravel 获取最后插入的 id

Poo*_*hna 3 php mysql laravel-5

我遇到了一个问题。我需要从表中插入的最后一个 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');
}
Run Code Online (Sandbox Code Playgroud)

等待回应。

Ste*_*ard 9

而不是使用

\DB::table('categories')->insert($insert);
Run Code Online (Sandbox Code Playgroud)

\DB::table('categories')->insertGetId($insert);
Run Code Online (Sandbox Code Playgroud)

insertGetId方法将插入数据,然后返回插入行的递增 ID。