LoginSignup
37

More than 5 years have passed since last update.

posted at

updated at

LaravelのFormで複数Submitボタンを設置したいときのハンドリング方法

この記事について

LaravelのBladeテンプレートでSubmitボタンを複数置く際のハンドリング方法について紹介します。

どんなときに困ったか

  • 入力データが同じだからと一画面で追加・編集・削除するような画面
  • 追加するときは追加処理を、編集するときは編集処理をと分けたい

解決方法

*.blade.phpで各ボタンに名前をつけ,その名前を元にControllerで振り分ける。

Http/routes.php
...
Route::post('hoge/form', 'HogeController@postHoge');
...
resources/views/hoge/form.blade.php
...
<label class="form-inline">
    <input class="btn btn-default form-control" type="submit" name="foo" value="Foo">
    <input class="btn btn-default form-control" type="submit" name="bar" value="Bar">
</label>
...
Http/Controllers/HogeController.php
...
public function postHoge(){
    if (Input::get('foo')){
        $this->foo();
    }elseif (Input::get('bar')){
        $this->bar();
    }
}

public function foo(){
    ....
}

public function bar(){
    ....
}
...

参考

php - Laravel 4 - two submit buttom in one form and have both submits to be handled by different actions - Stack Overflow

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
37