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

meredevelopment's avatar

Help re-populating checkbox array in Blade template.

Hi, I'm trying to setup an array of roles checkboxes for a user management view. I have this, which works:

@php
    $userroles = $user->roles->pluck('id')->toArray();
@endphp

@foreach ($roles as $role)
    <div>
        <input class="checkbox" type="checkbox" name="roles[]" id="check-box-{{$role->id}}" value="{{ $role->id }}" {{ in_array($role->id, $userroles) ? "checked" : "" }} />
        <label for="check-box-{{$role->id}}">{{$role->display_name}}</label>
    </div>
@endforeach 

Knowing how many helper functions and methods Laravel has, I'm pretty sure I'm retrieving the previously set value a goofy way. Using in_array($role->id, $userroles) seems wrong.

Also, if possible I'd like to avoid the @php block. How SHOULD I be re-populating checkboxes?

Any ideas or corrections gratefully received!

0 likes
3 replies
rawilk's avatar
rawilk
Best Answer
Level 47

I think in_array() works just fine for this situation, but you could also use isset() as well. For that you would do something like:


{{ isset($userroles[$role->id]) ? "checked" : "" }}

If you'd like a more "Laravel" way of doing it, you could also use the array helper array_has() helper function provided by Laravel. Using this function, you would do something like this:


{{ array_has($userroles, $role->id) ? "checked" : "" }}

I personally prefer to do this kind of logic with a function on the User model. Below is some code that I would put on the User model and then the blade to accomplish this:

User Model


/**
 * Determine if the user has the given role.
 *
 * @param mixed $role
 * @return boolean
 */
 public function hasRole($role)
 {
    // Assuming your role model has a 'name' field on it
    if (is_string($role)) {
        return $this->roles->contains('name', $role);
    }
    
    // If you pass a role id in, check by id
    if (is_numeric($role)) {
        return $this->roles->contains('id', $role);
    }
    
    // If you pass a Role object in, compare each of your role's id to this one's
    foreach ($this->roles as $user_role) {
        if ($user_role->id == $role->id) {
            return true;
        }
    }
    
    // If nothing matched, return false
    return false;
 }

Then in the blade do this:


{{ $user->hasRole($role->id) ? "checked" : "" }}

To avoid the @php block, you could move that query to your controller and pass it down to the blade.


// This code should be in whatever function is directing Laravel to show your blade.
$user = 'your user query';
$roles = 'your roles query';
$userroles = $user->roles->pluck('id')->toArray();

return view('your.view.name', compact('user', 'roles', 'userroles'));
3 likes
meredevelopment's avatar

Hi Wilk, Thanks a lot, that was really useful and I'm now passing the array to the view with return view("manage.users.edit")->with('user', $user)->with('roles', $roles)->with('userroles', $userroles);

I did try and use the hasRole() function but when I try to call it with $user->hasRole() in the view I get nothing back, just null.

So I attempted to call it explicitly like so: App\Http\Controllers\UserController::hasRole() which then gives me a "Non-static method App\Http\Controllers\UserController::hasRole() should not be called statically" error. If I make the function static I get "Using $this when not in object context" error. So.. I'm going in circles now.

You've already helped a load, so don't worry if you're too busy, but IF you have time, I'd love to know how to get the scope right on this hasRole() function.

Thanks! Ben

rawilk's avatar

@meredevelopment for the hasRole() function, I didn't put it in a controller but instead I put it on the User model itself. That's why I was able to call it like $user->hasRole()

2 likes

Please or to participate in this conversation.