1

Given the following code, all my checkboxes (roles) are marked as checked, even though the user only has one role.

Using Laravel and Spatie Laravel Permissions Package.

I tried the same code in Tinker and it comes back with True, False, False, so it should be working...

@foreach ($roles as $role)
    <div>
        <label>
            <input type="checkbox" value="{{ $role->name }}" checked="{{ $user->hasRole($role->name) ? 'checked' : '' }}">
            <span>
                {{ $role->name }}
            </span>
        </label>
    </div>
@endforeach

1 Answer 1

3

Change your input element to:

<input type="checkbox" value="{{ $role->name }}" {{ $user->hasRole($role->name) ? 'checked' : '' }}>

Checkboxes have a simple checked attribute, you don't assign a value to it.

W3 Example

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this! I am so sure I tried that first, but then in my annoyance I must have changed another part of the code that fixed the problem and then proceeded to break it again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.