我想在表单中使用两个提交按钮,第一个是保存并将您重定向到主页,第二个是保存并再次将您重定向到同一表单,我使用public function store进行保存,这是我的表格
<form method="post" action="{{route('return.store')}}" enctype="multipart/form-data">
<div class="form-group">
<button type="submit" class="btn btn-md" name="submit">Verstuur</button>
</div>
</form>
和我的控制器
public function store(Request $request)
{
$this->validate($request, [
'image' => 'image|nullable|max:1999'
]);
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalExtension();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . Carbon::today()->toDateString() . '.' . $extension;
$request->file('image')->storeAs('public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$retour = new Retour();
$retour->firmaname = request('firmaname');
$retour->contactperson = request('contactperson');
$retour->email = request('email');
$retour->ordernumber = 653 .request('ordernumber');
$retour->articlenumber = request('articlenumber');
$retour->return_quantity = request('return_quantity');
$retour->return_quality = request('return_quality');
$retour->return_reason = request('return_reason');
$retour->images = $fileNameToStore;
$retour->save();
return redirect('/return')->with('message', 'Je retourmelding is succesvol verzonden');
}
您可以在提交按钮中使用具有不同值的name
属性,然后根据该值执行预期的操作:
<form method="post" action="{{route('return.store')}}" enctype="multipart/form-data">
<button type="submit" name="action" value="homepage">Save</button>
<button type="submit" name="action" value="same_form">Save</button>
</form>
在控制器中,您可以像这样操作,
public function store(Request $request)
{
if($request->action == 'homepage'){
return redirect('homepageurl');
}
if($request->action == 'same_form'){
return redirect()->back();
}
}