Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pedroroccon's avatar

Blade checkbox always checked, but my parameter returns "false"

Hello guys, thanks for the interest in my topic.

I think that I'm stuck in a simple step.

I have a form to edit the current users in my system, so in that form I have a checkbox that checks if the user has a specific "role" (Entrust Roles). If the user already have the role, then my checkbox needs to be automatically checked. See the code:

@foreach($roles as $role)
    <div class="col-md-4">
        <div class="checkbox">
            <label>{!! Form::checkbox('roles[]', $role->id, $user->hasRole($role->name)) !!}{{ $role->display_name }}</label>
        </div>
    </div>
@endforeach

The $roles variable comes from my UserController.php

/**
* Show the form for editing the specified resource.
 *
* @param  App\User  $user
* @return Response
 */
public function edit(User $user)
{
    $roles = Role::orderBy('display_name')->get();
    return view('user.edit', compact('user', 'roles'));
}

I used the command dd($user->hasRole($role->name)) to see the output. It returns me "false", but when I'm inserting the $user->hasRole($role->name) in the third argument of the checkbox, my app always checks the checkbox.

Why this error happens even my $user->hasRole($role->name) returning false?

Thank you guys!

0 likes
12 replies
jlrdw's avatar

Don't know much about blade but a checkbox is a one or zero so I

<tr>
                <td>Adopted:</td>
                <?php
                if ($data['row'][0]->adopted == 1) {
                    echo "<td>";
                    echo "<input type=\"checkbox\" name=\"adopted\" id=\"adopted\" value=\"1\" checked />";
                    echo "</td>";
                } else {
                    echo "<td>";
                    echo "<input type=\"checkbox\" name=\"adopted\" id=\"adopted\" />";
                    echo "</td>";
                }
                ?>
            </tr>

and test for a check

$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

If not checked, nothing passes in php, so that will be a nothing value.

tykus's avatar

Does hasRole() return a boolean? A string would be truthy.

pedroroccon's avatar

@jlrdw Thanks for the answer. I understand your logic, but I really need the code written in Blade Template.

pedroroccon's avatar

@tykus_ikus I used the var_dump on my hasRole() and it returns me bool(false), so... Isn't a string. But the checkbox stills checking

bashy's avatar

Use

<div class="checkbox">
    <label>{!! Form::checkbox('roles[]', $role->id, ($user->hasRole($role->name) ?: null)) !!} {{ $role->display_name }}</label>
</div>

That is crazy @jlrdw :P

2 likes
tykus's avatar
     _.-^^---....,,--
 _--                  --_
<                        >)
|                         |
 \._                   _./
    ```--. . , ; .--'''
          | |   |
       .-=||  | |=-.
       `-=#$%&%$#=-'
          | ;  :|
 _____.,-#%&$@%#&#~,._____ 
1 like
pedroroccon's avatar

@bashy I've tried this way, but no success :(. Same problem. I checked my database to be sure, and my current user really doesn't have any role.

jlrdw's avatar

@bashy I don't use blade, so that is a standard way of checking or un-ckecking in html5, which is what blade converts to any way. How do you do it (not using blade)? A zero(0) is unckecked, and a one(1) stored in database is for checked. Now has something changed I don't know about?
At least up until dec 2015, if a checkbox is empty nothing is passed in querystring or the post params. Has there been a change to this?

tykus's avatar
tykus
Best Answer
Level 104

@pedroroccon - your original syntax is okay, but you need to change the name of the checkboxes to something that is not also a relationship name, e.g. selected_roles[](you can probably come up with something better)

The problem is FormBuilder attempts to determine the checked state of a checkboxes based on the properties of the User instance, however, it doesn't handle relationships correctly and ultimately the $user->roles collection is cast to a boolean (which is always true). I'll submit a PR to LaravelCollective tomorrow.

1 like
pedroroccon's avatar

@tykus_ikus Oh my god! I would never think that the problem was the name. You saved my life! Thank you very much. Here is the working code:

@foreach($roles as $role)
    <div class="col-md-4">
        <div class="checkbox">
            <label>{!! Form::checkbox('user_roles[]', $role->id, ($usuario->hasRole($role->name) ?: null)) !!}{{ $role->display_name }}</label>
        </div>
    </div>
@endforeach

Thanks to all who answered

bashy's avatar

@jlrdw What you put is wrong? You always need a value but just toggle the "checked".

I would do this. Unless it's a checkbox that needs a value, you'd just be using 1 or "yes", depending on what you want to use with the POST data.

<input type="checkbox" name="adopted" id="adopted" value="1"<?php echo ($data['row'][0]->adopted == 1 ? ' checked' : ''); ?>>
jlrdw's avatar

@bashy I like your shortcut way of doing that, I will definitely implement that thanks.
This line was just controller example prior to an update.

$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

Top part view list example.

Please or to participate in this conversation.