我有form
。此操作是:
<form action="{{url('career/save/{id}')}}" method="POST" enctype="multipart/form-data">
路线:
Route::group(['prefix' => 'career'], function () {
Route::get('apply', ['as' => 'addApplicant', 'uses' => 'ApplicantController@create']);
Route::post('save', ['as' => 'saveApplicant', 'uses' => 'ApplicantController@store']);
Route::get('confirmation/{id}', ['as' => 'confirmationMsg', 'uses' => 'ApplicantController@show']);
});
控制器:
public function store(Request $request)
{
$applicant = new Applicant();
$applicant['name'] = $request->input('name');
$applicant['sex'] = $request->input('sex');
$applicant['marital_status'] = $request->input('marital_status');
$applicant['date_of_birth'] = $request->input('date_of_birth');
$applicant['email'] = $request->input('email');
$applicant->save();
\Session::flash('flash_message','Application has been successfully submitted.');
return redirect(route('confirmationMsg'));
}
public function show($id)
{
$applicantData = Applicant::whereId($id)->first();
return view('applicant.confirmation',compact("applicantData"));
}
如果每个申请人/每个人都提交此表单,那么它将创建一个ID并在确认页面中将其重定向到他提交的数据。
但提交后我得到了:
../公共/职业/保存/%7Bid%7D
我可以手动输入../public/career/confirmation/14
并完美地完成工作。现在我想要在提交表单后重定向此../public/career/confirmation/ID
。 ID 是动态的。我该怎么做?提前致谢。
答案 0 :(得分:3)
您可以在提交后重定向
return redirect()->to('career/confirmation/'.$id); //pass your dynamic id
或通过这样做
return redirect()->route('confirmationMsg',['id'=>$id]); //pass your dynamic id
这样做,$id = $applicant->id;
替换你的这一行
return redirect(route('confirmationMsg'));
这个
return redirect()->route('confirmationMsg',['id'=>$id]); //pass your dynamic id
答案 1 :(得分:2)
提供您用于表单操作的邮寄路线,即career/save/{id}
。你可以看到其他修正。
在$applicant->save();
操作后,$applicant->id
应该是最后插入的申请人ID,因此请在redirect()
时使用。
我们进一步翻译%7Bid%7D
,{id}
。意味着id
在这里起作用。
尝试以下代码:
public function store(Request $request)
{
$applicant = new Applicant();
$applicant['name'] = $request->input('name');
$applicant['sex'] = $request->input('sex');
$applicant['marital_status'] = $request->input('marital_status');
$applicant['date_of_birth'] = $request->input('date_of_birth');
$applicant['email'] = $request->input('email');
$applicant->save();
//Get last inserted applicant id...
$applicantId = $applicant->id;//Check your correspond table for primary key column label is 'id'.
\Session::flash('flash_message','Application has been successfully submitted.');
return redirect(route('confirmationMsg', [id => $applicantId]))
}
这对你有用......!
答案 2 :(得分:0)
在$applicant->save();
之后,获取ID
$id = $applicant->id;
然后重定向,return redirect(route('confirmationMsg', [id => $id]));
答案 3 :(得分:0)
重定向到特定路径:
return redirect('/routepath');
重定向到当前路径:
return redirect()->back();