具有两个提交按钮的表单laravel 6

问题描述 投票:0回答:1

我想在表单中使用两个提交按钮,第一个是保存并将您重定向到主页,第二个是保存并再次将您重定向到同一表单,我使用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');
    }

laravel forms button controller submit
1个回答
0
投票

您可以在提交按钮中使用具有不同值的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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.