How to show old data of dynamic checkbox in Laravel?

How to show old data of dynamic checkbox in Laravel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Checkbox State in Laravel Forms

Rendering dynamic checkboxes presents a distinct challenge compared to text inputs or radio buttons. While you can easily display existing data for single-value fields like name or gender, multi-select inputs require handling arrays. The core logic remains the same as standard form binding, but the syntax must account for how PHP constructs request arrays when multiple inputs share the same name with brackets, such as hobby[].

To restore previous values, you need to compare the incoming request array against your database model. In Laravel, the old helper function is designed to retrieve input from the latest request that failed validation or was abandoned. When dealing with arrays, passing a single string value to old() will not work as expected because it expects an array when the input name contains brackets.

The correct approach involves using old('hobby[]', ...) and providing a default array if no previous selection exists. You must then iterate through your available options (football, basketball, swimming) and check if each specific value exists within that retrieved array. If it does, you apply the checked attribute.

Here is the implementation for your hobby section:

<div class="form-check">
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="1" 
            @if(in_array(1, old('hobby[]', []))) checked @endif> football
    </label>
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="2" 
            @if(in_array(2, old('hobby[]', []))) checked @endif> basketball
    </label>
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="3" 
            @if(in_array(3, old('hobby[]', []))) checked @endif> swimming
    </label>
</div>

The critical change here is the use of in_array() combined with a default empty array []. When the form is submitted and validation fails, Laravel populates the request data. If you submit an array like ['hobby' => ['1', '3']], old('hobby[]') returns that array. The in_array function checks if the specific checkbox value (e.g., 1) is present in that list.

If no previous data exists or the field was not submitted, old() returns an empty array. Attempting to check against a single string variable, like $student->hobby, would be incorrect here because your database likely stores hobbies as a string representation of an array (e.g., "1,3") or you might be using a relationship. However, the standard Laravel pattern assumes old() handles the retrieval from the request superglobal ($_POST), while $student->hobby retrieves the saved state from your model.

You must decide which source of truth to prioritize for the "checked" state:

  1. Request Data (old()): This is best for handling form re-submission errors. If a user fills out hobbies, clicks submit, and validation fails, the hobbies should remain checked so they don't have to re-enter them.
  2. Model Data ($student->hobby): Use this only if you are on a view that displays an existing student record but has not yet received a form submission in the current request cycle (e.g., a "show" page). In that specific case, you would need to parse your model's hobby attribute into an array first, assuming it is stored as a comma-separated string.

For instance, if $student->hobby contains the string "1,3" and you want to display the form initially populated with this data on a view without a POST request, the logic changes:

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" 
    @if(in_array(1, explode(',', $student->hobby))) checked @endif> football

Using explode converts the string from the database into an array so in_array can function correctly. However, be cautious with this approach if your model uses a JSON column or relationship collection instead of a simple string; accessing $student->hobbies directly might return a collection that needs to be converted to an array using pluck() or toArray().

If you are working on a larger application where managing these manual checks becomes unwieldy, consider leveraging the Laravel validation engine. You can validate the incoming hobby[] array and use $errors->get('hobby[]') to populate checkboxes with error states if necessary. For detailed guidance on building robust forms in Laravel, you might find additional resources at https://laravelcompany.com.

Our network