29

I want to set check-boxes state from database, so I write,

{{ Form::checkbox('asap', null, $offer->asap) }}

But if I want to set 'id' to the check-box like

{{ Form::checkbox('asap', null, $offer->ASAP, array('id'=>'asap')) }}

It always set my check-box state to true. (Before user select it)

So question how set 'id' in blade check-boxes when check-box state is set before user select it?

Lara Gallassi
  • 157
  • 1
  • 8
lggg3
  • 527
  • 2
  • 5
  • 12

4 Answers4

53

I know this question was answered before, in this one I am going to explain step by step how to implement checkboxes with Laravel/blade with different cases ...

So in order to load your checkboxes from the database or to test if a checkbox is checked :

First of all you need to understand how it works, as @itachi mentioned :

{{ Form::checkbox( 1st argument, 2nd argument, 3rd argument, 4th argument ) }}

  • First argument : name
  • Second argument : value
  • Third argument : checked or not checked this takes: true or false
  • Fourth argument : additional attributes (e.g., checkbox css classe)

Example :

{{ Form::checkbox('admin') }} 
//will produces the following HTML
<input name="admin" type="checkbox" value="1">

{{ Form::checkbox('admin', 'yes', true) }}
//will produces the following HTML
<input checked="checked" name="admin" type="checkbox" value="yes">

How to get checkboxes values ? ( in your controller )

Methode 1 :

public function store(UserCreateRequest $request)
{

   $my_checkbox_value = $request['admin'];

  if($my_checkbox_value === 'yes')
     //checked
  else
     //unchecked
  ...
}

Methode 2 :

if (Input::get('admin') === 'yes') {
    // checked
} else {
    // unchecked
}

Note : you need to assign a default value for unchecked box :

if(!$request->has('admin'))
{
    $request->merge(['admin' => 0]);
}

this is nice right ? but how could we set checked boxes in our view ?

For good practice I suggest using Form::model when you create your form this will automatic fill input values that have the same names as the model (as well as using different blade Form:: inputs ..)

{!! Form::model( $user, ['route' => ['user.update', $user->id], 'method' => 'put' ]) !!}
    {!! Form::checkbox('admin', 1, null) !!}
{!! Form::close() !!}

You can also get it like this :

{{ Form::checkbox('admin',null, $user->admin) }}

Okey now how to deal with :

  • multiples checkboxes
  • Add css classe
  • Add checkbox id
  • Add label

let's say we want to get working days from our database Working days

$working_days = array( 0 => 'Mon', 1 => 'Tue', 2 => 'Wed', 
                       3 => 'Thu', 4 => 'Fri', 5 => 'Sat', 6 => 'Sun' );

@foreach ( $working_days as $i => $working_day )
{!! Form::checkbox( 'working_days[]', 
                  $working_day,
                  !in_array($working_days[$i],$saved_working_days),
                  ['class' => 'md-check', 'id' => $working_day] 
                  ) !!}
{!! Form::label($working_day,  $working_day) !!}
@endforeach
//$saved_working_days is an array of days (have 7 days, checked & unchecked)

I've spent sometime to figure out how to deal with multiple checkboxes I hope this can help someone :)

Mohamed Salem Lamiri
  • 5,767
  • 6
  • 34
  • 47
16

3rd argument decides whether checkbox is checked or not. So probably $offer->ASAP (or $offer->asap is true (or not false or not null). If you want to to make checkbox unchecked either set it to false, or don't use 3rd argument (set to to null or false):

{{ Form::checkbox('asap',null,null, array('id'=>'asap')) }}

EDIT

Another possibility is that you have on your page some custom JavaScript code that finds element by asap id and checks this checkbox. So when you don't set id, JavaScript cannot check it, but when you set this id, the checkbox will be checked by JavaScript.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • $offer->ASAP take value from database and it is 0 (FALSE), so if i wrote {{ Form::checkbox('asap',null,$offer->asap) }} everything works fine, but if i want ad some id it goes always TRUE – lggg3 Nov 17 '14 at 13:23
  • @lggg3 Are you sure for that? Do you have any JavaScript on your page? It's possible that JavaScript finds element by `asap` id (if you have such script) and checks it (of course no matter of what you passed in Laravel) – Marcin Nabiałek Nov 17 '14 at 13:27
  • 1
    You was write i found JavaScript in the code that allways set boxes as true – lggg3 Nov 17 '14 at 13:44
3

in FormBuilder.php

    public function checkbox($name, $value = 1, $checked = null, $options = array())
{
    return $this->checkable('checkbox', $name, $value, $checked, $options);
}
  • 1st param: name
  • 2nd : value
  • 3rd : checked or not (i.e. null, false or true)
  • 4th : attributes.

{{ Form::checkbox('asap',null,$offer->ASAP, array('id'=>'asap')) }}

your order is wrong.

it should be,

{{ Form::checkbox('asap',$offer->ASAP, null, array('id'=>'asap')) }}
itachi
  • 6,323
  • 3
  • 30
  • 40
  • Wrong your ordering is bad. Look agen to your post – lggg3 Nov 17 '14 at 13:32
  • can you tell the mistake above? – itachi Nov 17 '14 at 13:36
  • You write it should be {{ Form::checkbox('asap',$offer->ASAP, null, array('id'=>'asap')) }} but realy it should be {{ Form::checkbox('asap',null,$offer->ASAP, array('id'=>'asap')) }} find the mistake where in code I find javascript that allways checked the box – lggg3 Nov 17 '14 at 13:43
  • 1
    it isn't wrong. it works in your case because you are setting the `$offer->asap` as `0` or `1` only (or equivalent). just look at the method. your order is wrong. but it works because of what you are returning from the database. if you set your value to 'yes' or 'no', you will get unexpected result. – itachi Nov 17 '14 at 13:46
3

Good luck!

 <div class="togglebutton">
       <label>
       @if ($programmer->play === 1)
          <input type="checkbox" name="play" checked="">
       @else
          <input type="checkbox" name="play" {{ old('play') ? 'checked' : '' }} >
       @endif
          Play
      </label>

 </div>
Cherma Ramalho
  • 373
  • 3
  • 7