How to show checkboxes as checked when values are set in the database in Laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Show Checkboxes as Checked When Values are Set in the Database in Laravel

As developers working with relational data in web applications, one of the most common tasks is managing state—ensuring that form inputs accurately reflect the data stored in the database. A frequent scenario arises when dealing with many-to-many relationships, such as assigning user roles, where we need to pre-select options based on existing database entries.

This guide will walk you through the practical steps of how to achieve this in a Laravel application using Blade templates and Eloquent data structures. We will solve the specific problem of displaying assigned roles as checked checkboxes when editing a user profile or role assignment screen.

Understanding the Challenge: Data Synchronization

You are facing a classic synchronization problem. You have two distinct sets of data:

  1. Available Roles ($roles): The complete list of all possible options to display in the form.
  2. Assigned Roles ($userRoles): A list detailing which roles the current user is already linked to in the database.

The goal is to iterate over the available roles and, for each role, check if its ID exists within the set of assigned role IDs before rendering the checkbox.

The Solution: Conditional Rendering in Blade

The solution lies in iterating through your available roles and using a conditional statement (@if) combined with an array method (like in_array or checking membership within a collection) to determine the initial state of the checkbox.

Let’s assume you have correctly loaded your data via Eloquent relationships, perhaps eager loading the pivot table data to get $userRoles.

Step 1: Prepare the Assigned Role IDs

First, since you are iterating over the roles, it is most efficient to prepare a simple, searchable array of the IDs that the user already possesses.

// Assuming $userRoles is the collection fetched from your model relationship
$assignedRoleIds = $userRoles->pluck('role_id')->toArray();

Step 2: Iterate and Apply Conditional Logic

Now, within your Blade view, you iterate through all available roles ($roles). Inside this loop, you check if the current $role->id is present in the $assignedRoleIds array. If it is, we render the checkbox with the checked attribute set to true.

Here is how you would modify your existing loop:

@foreach ($roles as $role)
    <div class="form-check">
        {{-- Check if the current role ID exists in the array of assigned IDs --}}
        <input type="checkbox" 
               name="role[]" 
               value="{{ $role->id }}" 
               {{ in_array($role->id, $assignedRoleIds) ? 'checked' : '' }}>

        <label for="role_{{ $role->id }}">{{ $role->name }}</label>
    </div>
@endforeach

Explanation of the Logic

  1. $assignedRoleIds = $userRoles->pluck('role_id')->toArray();: This line efficiently extracts only the IDs from your assigned roles into a simple, fast-lookup array.
  2. in_array($role->id, $assignedRoleIds): For every role in the $roles list, we check if its unique ID exists within the $assignedRoleIds array.
  3. ? 'checked' : '': This is the ternary operator. If in_array() returns true (meaning the role is assigned), the expression evaluates to 'checked', and the HTML attribute becomes checked. If it returns false, an empty string is used, meaning no attribute is added, leaving the box unchecked by default.

This approach ensures that the form accurately reflects the data stored in your database, making the user experience intuitive and minimizing errors during submissions. This level of data manipulation is a cornerstone of effective data handling in any robust framework like Laravel. For more advanced data fetching and structuring within your application, exploring Eloquent relationships and query builders often provides powerful tools (check out the resources available at https://laravelcompany.com).

Conclusion

By strategically checking for the existence of a record in your assigned roles collection before rendering each checkbox, you can seamlessly synchronize your form state with your backend database. This technique is highly practical, scalable, and ensures data integrity when managing complex relational data. Implementing this simple conditional logic transforms a static list into an interactive management tool.