Skip to content
Advertisement

Laravel checkbox is not submitted to controller

I have a simple form in a view like this:

<form action="{{ URL::route('admin.x') }}" method="POST">
    <input type="text" value="b" name="title" />
    <input type="text" value="c" name="type" />
    <input type="text" value="d" name="postfix" />
    <input type="checkbox" name="check" value="ss" />
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

I used

dd(Input::all());

for checking all of values posted to controller from view. the result is like below and I want to know how to post checkbox value with form.

array:4 [▼

“title” => “”

“type” => “”

“postfix” => “”

“_token” => “kSM3pO11KOOWQcCx4PeWcbi4r4AsFx0rSGQoVFtG”

]

Advertisement

Answer

A checkbox input is not submitted to the server side script if not checked by the user or by a client side script. So, in these cases, the input isn’t sent to your controller

In laravel, if you want to ‘normalize’ somehow the situation when the input isn’t sent, you can create a field by yourself:

$data = Input::all();

if ( ! isset($data['check']) )
    $data['check'] = false; 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement