Passing a boolean value from checkbox

Passing a boolean value from checkbox

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

Stefan Bogdanescu

Founder & Senior Architect

Passing a Boolean Value from Checkbox

Handling boolean inputs in Laravel forms often trips up beginners because the relationship between HTML checkboxes and database booleans isn't one-to-one by default. The core issue lies in how HTML forms transmit data versus how Eloquent expects to save that data.

When you render a checkbox using <input type="checkbox">, the browser only sends a request parameter if the box is checked. If the user leaves the box unchecked, no value is sent to your controller at all. Conversely, if the box is checked, the value sent depends entirely on the value attribute you assigned in the Blade template.

In the example provided, the view renders the checkbox like this:

<input type="checkbox" name="is_featured" class="switch-input" value="{{old('is_featured')}}">

There are two distinct problems here. First, using old('is_featured') as the default value is incorrect for a checkbox input. The old() helper retrieves the last validated value from the session to repopulate inputs on validation failure. However, setting an old boolean value as the HTML attribute does not visually indicate whether the checkbox should appear checked or unchecked in the browser. Second, if the user checks this box, the request payload will contain something like ['is_featured' => 'on'] (the default Laravel value) or whatever string you explicitly set. Your database schema expects a strict boolean (true or false), but your controller is likely saving the raw string 'on' or even an empty string if the box is unchecked.

To resolve this, you must normalize the incoming request data before assigning it to the model. The most robust approach is to check whether the key exists in the request and, if so, determine if its value is truthy.

Update your PostController as follows:

public function store(Request $request)
{
    $rules = [
        'title' => ['required', 'min:3'],
        'body' => ['required', 'min:5']
    ];
    
    $request->validate($rules);

    $user_id = Auth::id();
    $post = new Post();
    $post->user_id = $user_id;
    $post->title = request('title');
    $post->body = request('body');

    // Handle the boolean logic explicitly
    if ($request->has('is_featured')) {
        $post->is_featured = $request->boolean('is_featured');
    } else {
        // Default to false if not submitted, or handle null based on schema preference
        $post->is_featured = false; 
    }

    $post->save();

    $posts = Post::all();
    return view('backend.auth.post.index', compact('posts'));
}

The request()->boolean() helper is crucial here. It intelligently converts the string 'on', '1', or any non-empty truthy value into true. If the key is missing (meaning the checkbox was unchecked), it returns false. This ensures that regardless of what the HTML sends, your Eloquent model receives a valid boolean type compatible with the schema.

Similarly, when updating an existing post in your update method, apply the same logic:

public function update(Request $request, Post $post)
{
    // ... validation ...

    if ($request->has('is_featured')) {
        $post->is_featured = $request->boolean('is_featured');
    } else {
        $post->is_featured = false;
    }

    $post->save();
}

It is also worth noting that your schema definition includes nullable() on the boolean columns. While valid, standard Laravel practice often avoids nullable booleans unless there is a specific business requirement for them to be null. If you strictly want false as the default, removing nullable() and keeping default(false) provides better type safety and prevents accidental null values from persisting if logic ever slips up.

For more complex form handling scenarios or if you are building reusable components, consider how Laravel's built-in helpers can simplify this pattern further. If you need deeper insights into managing request payloads and validation rules across your application architecture, resources like https://laravelcompany.com can offer structured guidance on best practices for securing and sanitizing user input.

Our network