5

I'm trying to implement checkbox and laravel collective in my form but I get only single value in form, any ideas how to fix it

{!! Form::open(array('action'=>'UserController@updateInfo','method'=>'post')) !!}
 Workdays:
<br>
{!! Form::label('monday', 'Monday') !!}
{!! Form::checkbox('workday', 'monday') !!}
<br>
{!! Form::label('tuesday', 'Tuesday') !!}
{!! Form::checkbox('workday', 'tuesday') !!}
<br>
{!! Form::label('wednesday', 'Wednesday') !!}
{!! Form::checkbox('workday', 'wednesday') !!}
<br>
{!! Form::label('thursday', 'Thursday') !!}
{!! Form::checkbox('workday', 'thursday') !!}
<br>
{!! Form::label('friday', 'Friday') !!}
{!! Form::checkbox('workday', 'friday') !!}
<br>
{!! Form::label('saturday', 'Saturday') !!}
{!! Form::checkbox('workday', 'saturday') !!}
<br>
{!! Form::label('sunday', 'Sunday') !!}
{!! Form::checkbox('workday', 'sunday') !!}
<br>
{!! Form::submit('Save', $attributes = ['class'=>'button']) !!}
{!! Form::close() !!}

when I print my request i only get single output (eg. selected monday friday I get only friday when request is processed)

also labels not working - ideas on that too?

ggoran
  • 630
  • 2
  • 12
  • 29

3 Answers3

4

You're using the same name (workday) for all your checkboxes. That's why it's only showing the last checkbox with that name

Just change all names to workday[] instead.

{!! Form::checkbox('workday[]', 'monday') !!}

This will return all selected checkbox in an array.

raz
  • 409
  • 6
  • 17
  • thanks it works... didn't expect that I need set array, thanks. any idea howto set labels – ggoran Nov 01 '16 at 21:53
  • Your labels seems to be fine. What are you getting? – raz Nov 02 '16 at 04:55
  • if you're having any further issues you can refer to these sites: [laravel recipes: labels](http://laravel-recipes.com/recipes/154/creating-a-form-label-element) or [laravel docs: labels](https://laravel.com/docs/4.2/html#labels) – raz Nov 02 '16 at 04:59
  • labels are there but when i click them checkbox is not clicked, I need to click on checkbox in order to select item, can't click text for checkbox toggle – ggoran Nov 02 '16 at 10:26
  • labels are linked to the `id` of the checkbox, not the `name`. Just add an `id` on your checkbox same with the name of your label: `{!! Form::checkbox('workday[]', 'sunday', null, ['id' => 'sunday']) !!}` – raz Nov 02 '16 at 16:03
  • Don't forget to add `null` on the third argument – raz Nov 02 '16 at 16:15
  • love it works, thanks on reference for laravel recipes, have useful stuffs there – ggoran Nov 02 '16 at 18:35
1

Try this: You used same name for every checkbox when you checked multiple checkbox you only get the last value.

{!! Form::open(array('action'=>'UserController@updateInfo','method'=>'post')) !!}
 Workdays:
<br>
{!! Form::label('monday', 'Monday') !!}
{!! Form::checkbox('workday[]', 'monday') !!}
<br>
{!! Form::label('tuesday', 'Tuesday') !!}
{!! Form::checkbox('workday[]', 'tuesday') !!}
<br>
{!! Form::label('wednesday', 'Wednesday') !!}
{!! Form::checkbox('workday[]', 'wednesday') !!}
<br>
{!! Form::label('thursday', 'Thursday') !!}
{!! Form::checkbox('workday[]', 'thursday') !!}
<br>
{!! Form::label('friday', 'Friday') !!}
{!! Form::checkbox('workday[]', 'friday') !!}
<br>
{!! Form::label('saturday', 'Saturday') !!}
{!! Form::checkbox('workday[]', 'saturday') !!}
<br>
{!! Form::label('sunday', 'Sunday') !!}
{!! Form::checkbox('workday[]', 'sunday') !!}
<br>
{!! Form::submit('Save', $attributes = ['class'=>'button']) !!}
{!! Form::close() !!}
-1

You're using the same name (workday) for all your checkboxes. That's why it's only showing the last checkbox with that name

{{ Form::checkbox('workday[]', 'monday') }}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Mas Hary
  • 96
  • 3