How to pass Checkbox value 0 if not checked and 1 if checked using array laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Checkbox Values in Laravel: Passing 0 When Unchecked

As developers working with web forms, one of the most common stumbling blocks involves handling boolean states—specifically, how to represent an unchecked state when submitting data via HTTP requests. When dealing with checkboxes in a Laravel application, we often run into a situation where if a box isn't checked, it simply doesn't appear in the request data, leading to missing values or unexpected errors in our controller logic.

This post will walk you through the most robust and idiomatic way to ensure that every checkbox submission results in a clear value: 1 if checked, and 0 if unchecked. We will leverage Laravel’s Blade templating capabilities to solve this elegantly before the data even hits your controller.

The Checkbox Value Trap Explained

Let's examine the scenario you presented. You are iterating over accounts and creating checkboxes:

html
@foreach($accounts as $acc)
    <input type="checkbox" value="1" name="account[{{ $acc->id }}]" @if($acc->published) checked @endif>
@endforeach

When a user submits this form, if an account is not published ($acc->published is false), the checked attribute is omitted entirely. Consequently, that specific input field is entirely absent from the submitted request data.

In your controller:

php
public function updateMon(Request $request) {
    // ... validation setup ...
    foreach($account as $acc => $val) {
        dd($val); // This will only see values for checked boxes!
    }
}

The reason you aren't getting a 0 is that the absence of the input field means the data never reached the request object for that specific key. To fix this, we need to ensure that every potential key exists in the submitted array, regardless of the checkbox state.

The Solution: Pre-populating Values in Blade

The solution lies in changing our approach from conditionally setting the checked state to unconditionally setting the value. We will always send a value (0 or 1) for every account in the request payload.

We can achieve this by removing the conditional checked attribute and instead using the ternary operator to set the value explicitly:

html
@foreach($accounts as $acc)
    <input type="checkbox" 
           name="account[{{ $acc->id }}]" 
           value="1" 
           {{ $acc->published ? 'checked' : '' }}>
@endforeach

Wait, that still doesn't solve the 0 problem directly on submission. The true trick is to set the value based on the checked state and ensure a default if nothing is selected:

The Correct Blade Implementation

To guarantee the presence of both 1 and 0 for every item, we adjust how we define the value. We will use the current state of $acc->published to define the value sent. If it's true, the value is 1; if it's false (unchecked), the value is 0.

html
@foreach($accounts as $acc)
    <input type="checkbox" 
           name="account[{{ $acc->id }}]" 
           value="{{ $acc->published ? '1' : '0' }}" 
           {{ $acc->published ? 'checked' : '' }}>
@endforeach

How This Works

  1. name="account[{{ $acc->id }}]": This maintains the necessary nested array structure for Laravel to bind the input correctly in the request.
  2. value="{{ $acc->published ? '1' : '0' }}": This is the critical part. Regardless of whether the box is checked or not, this expression generates a string: '1' if published, or '0' if not published. This value is always sent with the request.
  3. {{ $acc->published ? 'checked' : '' }}: We still use the conditional check here to ensure that the visual state of the checkbox matches the database record ($acc->published).

Controller Handling and Data Integrity

With the correct values being submitted, your controller logic becomes much cleaner and more reliable. You no longer need to worry about missing keys; you just process whatever data arrives.

php
use Illuminate\Http\Request;

class AccountController extends Controller
{
    public function updateMon(Request $request)
    {
        $request->validate([
            'account' => 'required|array',
            // We expect the values to be 0 or 1, so we validate them as integers.
            'account.*' => 'sometimes|integer', 
        ]);

        // The $request->input('account') will now contain all account IDs 
        // and their corresponding 0s or 1s.

        foreach($request->input('account') as $accountId => $value) {
            // $value will reliably be 0 (unchecked) or 1 (checked).
            dd("Account ID: " . $accountId . ", Status: " . $value);
        }
    }
}

By implementing this pattern, you ensure data integrity. This practice of handling form input cleanly is fundamental to building stable applications in Laravel. For more advanced tips on request handling and data binding within the framework, be sure to explore resources from https://laravelcompany.com.

Conclusion

Passing array values for checkboxes requires foresight. Don't rely on the browser omitting data when an item is unchecked; instead, force the submission of a default state (0) using conditional expressions within your Blade views. By ensuring that every checkbox sends either a 1 or a 0, you make your controller logic simpler, more robust, and easier to maintain.