How to save boolean values in laravel eloquent

How to save boolean values in laravel eloquent

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

Stefan Bogdanescu

Founder & Senior Architect

How to Save Boolean Values in Laravel Eloquent

When defining a boolean column in a migration using $table->boolean(), Laravel creates a standard database column that typically stores true or false. However, many legacy applications and specific database clients expect these columns to store integer values: 1 for true and 0 for false. This is a common friction point when migrating from older systems or when integrating with tools that parse raw SQL logs.

In the scenario provided, the migration correctly defines boolean fields like favicon, title, and image-optimization. The controller attempts to save data using $request->all(), which passes an associative array of inputs to Article::create(). While this works for standard text or integer fields, it fails to convert the checkbox input into the desired integer representation.

The core issue lies in how HTML checkboxes are submitted and how Laravel processes them before Eloquent hits the database. When a user leaves a checkbox unchecked, the browser does not send that key-value pair in the POST request body at all. Consequently, when create($request->all()) is called, the resulting array might look like this:

[
    'favicon' => false, // or even missing if using strict validation without defaults
    'title' => true,
    // 'image-optimization' is missing entirely
]

Laravel's default Eloquent model casting attempts to convert 1 and 0 into booleans when reading from the database (true/false). When writing, it converts true/false back into 1/0 if the column type is detected as an integer boolean, but standard $table->boolean() often results in a native boolean storage depending on the underlying driver (MySQL/MariaDB). If your database schema explicitly requires integers (TINYINT(1)), Eloquent will not automatically cast the incoming true/false values into 1/0 without explicit configuration.

To force the storage of 1 and 0, you have two primary approaches: adjusting the migration type or handling the casting within the model.

Option 1: Use tinyInteger in Migration

The most direct solution is to change the column definition in your migration. Instead of $table->boolean(), use $table->tinyInteger('favicon')->default(0). This instructs Laravel to treat the column as an integer. When you pass a boolean from the request, Eloquent's type casting logic will handle the conversion to 1 or 0 automatically because the underlying column is numeric.

Schema::create('quality_check', function (Blueprint $table) {
    $table->increments('id');
    $table->tinyInteger('favicon')->default(0);
    $table->tinyInteger('title')->default(0);
    $table->tinyInteger('image-optimization')->default(0);
});

This approach aligns with the behavior expected by many older systems and debugging tools. The default(0) ensures that if a checkbox is missing from the request, the value defaults to 0 rather than causing an error or saving as NULL.

Option 2: Explicit Type Casting in the Model

If you must keep the migration as $table->boolean(), you can force Eloquent to cast the values explicitly. However, this only affects reading; it does not guarantee writing 1/0 unless the column is actually stored as an integer type by the database engine. If your goal is strictly to see 1 and 0 in the database, changing the migration type is the robust solution.

Handling Missing Inputs

A critical detail often overlooked is the behavior of unchecked checkboxes. As noted, they do not appear in the request array. If you rely on $request->all() directly into create(), an unchecked box results in no data being sent for that field. If the database column has a default value (like 0), this is fine. If it does not, you will get a NULL or an error depending on your strictness settings.

To ensure consistency, consider defining attributes in your Request class or using mass-assignment rules to set defaults explicitly before saving. This guarantees that every boolean field has an explicit value—either 1 or 0—regardless of the frontend state.

By aligning the migration schema with the expected integer output and managing missing inputs via defaults, you ensure that your boolean flags behave predictably across different database clients and ORM versions.

Our network