1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelでinput,select,checkboxの値を取得する方法

Last updated at Posted at 2021-07-29

input

  • view
<input name="name" value="hoge">
  • controller
public function namae(Request $request)
{
    $name = $request->input('name');

    echo $name;
}
//出力結果 
//hoge

###select

  • view
<select name="age">
    <option value="20" selected>20歳</option>
    <option value="30">30歳</option>
</select>
  • controller
public function nenrei(Request $request)
{
    $age = $request->age;

    echo $age;
}
//出力結果
//20

###checkbox

  • view
<input type="checkbox" name="color[]" value="red" checked>
<input type="checkbox" name="color[]" value="yellow" checked>
<input type="checkbox" name="color[]" value="green">

name属性にカッコをつけてあげます。カッコつけないと値が1つしか入りません。

  • controller
public function iro(Request $request)
{
    $colors = $request->input('color');

//このとき$colorsは配列になっています。
    foreach($colors as $color){
        echo $color."\n";
    }
}
//出力結果
//red
//yellow

1
3
0

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
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?