route:
Route::get('/insert/{data}', 'IdataController@ins');
Route::post('/insert/{data}', 'IdataController@add_data');
controller:
public function ins($data = 1){
if($data == 1){
//display data
}elseif($data == 2){
// this place I need last insert id
}
}
public function add_data(Request $request, $data){
if($data == 1){
$r = $this->insert_my([
'name'=>$request->name,
'relationid'=>$request->relationid
]);
return $r;
}else($data ==2){
//next operations
}
}
private function insert_my($data){
$r= Insertmodel::create([
'name'=>$data['name'],
'relationid'=>$data['relationid']
]);
return Redirect::to('insert/2');
}
对我来说,最好的解决方案是从Model下载最后一个ID。但是怎么做呢?
id需要进入这个地方:
elseif($data == 2){
// this place I need last insert id
}
答案 0 :(得分:2)
你可以这样检索它:
private function insert_my($data){
$r= Insertmodel::create([
'name'=>$data['name'],
'relationid'=>$data['relationid']
]);
return Redirect::to('insert/2?id='. $r->id);
}
重定向后,它将在控制器中显示为$request->id
。
答案 1 :(得分:1)
尝试
<强>更新强>
$data = Model::all()->last()->id;
或
$data = Model::orderby('created_at')->first();
然后$data->id