Show checkbox checked from database in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Show Checkbox Checked from Database in Laravel: Mastering Relationship Checks for UI State
As senior developers working with Laravel, managing permissions and roles—especially when dealing with many-to-many relationships like ACL setups—often requires careful handling of relational data. When building administrative interfaces, accurately reflecting the current state stored in the database (like which permissions are assigned to a role) is crucial for a good user experience.
I see a common stumbling block when trying to populate checkboxes based on existing database entries: correctly comparing related Eloquent collections in your Blade views. This post will diagnose the issue you encountered while trying to display checked states for roles and permissions, provide the correct solution, and explain the underlying principles of efficient data retrieval in Laravel.
Diagnosing the Issue: Why Your Checkbox Logic Failed
You are attempting to check if a specific permission ID exists within the set of permissions associated with the current role. The error you encountered—Object of class Illuminate\Support\Collection could not be converted to int or Property [id] does not exist on this collection instance—stems from an incorrect comparison between a Laravel Collection and a scalar value.
Let's look at your problematic line:
{{ $role->permissions->pluck('id') == $permission->id ? 'checked' : '' }}
Here is what went wrong:
$role->permissionsreturns aIlluminate\Support\Collection(a collection of Permission models).->pluck('id')correctly extracts an array of IDs from that collection.- When you compare this entire array (
['1', '5', '10']) directly against a single value ($permission->id), the comparison logic doesn't yield the boolean result you expect in this context, leading to type conversion errors because PHP is confused about what it should be comparing.
The goal is not to compare the entire collection with a single ID, but rather to check for membership. We need to see if $permission->id exists within the set of IDs associated with the role.
The Correct Approach: Checking for Collection Membership
The most robust and idiomatic way to solve this is to use PHP's built-in collection methods or Eloquent's relationship features directly within your Blade template logic, ensuring you are checking if an ID exists in a related set.
Since you have already loaded the necessary data via your controller (which is excellent practice!), we can simplify the check significantly.
Corrected Implementation for Your View
Instead of relying on comparing two collections, we should check if the current permission ID exists within the role's permissions collection. The most efficient way to do this in Laravel/Blade is by using the contains method on the related collection or by checking against a pre-loaded array of IDs.
Assuming your $role->permissions relationship loads the related models, here is how you can correctly check for membership:
@foreach($permissions as $permission)
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" name="permissions[]" value="{{$permission->id}}"
{{ $role->permissions->pluck('id')->contains($permission->id) ? 'checked' : '' }}
>
<span>{{$permission->display_name}} <em>({{ $permission->description}})</em></span>
</label>
</div>
@endforeach
Explanation of the Fix
The critical change is the use of the contains() method on the collection derived from $role->permissions.
$role->permissions->pluck('id'): This extracts an array of all permission IDs assigned to the role (e.g.,[1, 5]).->contains($permission->id): This method efficiently checks if the current permission's ID ($permission->id) is present within that array of role IDs.
This approach cleanly resolves the type mismatch and correctly determines whether a specific permission has been assigned to the role, allowing you to accurately reflect the checked state from your database records. Following principles like these ensures that your application remains clean, performant, and adheres to Laravel's conventions, much like building robust systems on platforms like those offered by Laravel Company.
Conclusion
Managing complex data relationships in a dynamic interface requires precise logic. By understanding how Eloquent collections interact with Blade expressions, you can move past confusing type errors and implement clean, efficient solutions. Always remember to leverage the built-in methods of your collection objects—like pluck(), whereIn(), or contains()—to perform relational checks effectively. Mastering these details is what separates functional code from truly robust application development.