How to save boolean values in laravel eloquent

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Saving Boolean Values in Laravel Eloquent Models Introduction ----------------------- In this blog post, we will cover how to save boolean values in Laravel Eloquent models when dealing with checkboxes within forms. We'll explore the basics of working with checkboxes and the necessary adjustments that need to be made to ensure proper data saving. By the end of this article, you should feel confident in your understanding and ability to implement the correct methods for saving boolean values from form submissions. Form Inputs and Laravel Eloquent Models ----------------------- In Laravel, when working with forms and models, we typically use Form requests to validate input data before creating or updating a model instance. In our example, we have created a form with three checkboxes: one for Favicon, another for Page Title, and the last for Image Optimization. These checkboxes are used to save their respective boolean values in the database. Form Validation with Checkboxes ----------------------- By default, Laravel will not add validation rules for boolean fields when using Form requests since they don't require user input. However, ensuring that these boolean values are saved as 1 (checked) or 0 (unchecked) is essential to avoid any unexpected issues during data retrieval or presentation stages. To ensure that the checkboxes behave in this manner, we can make some adjustments to our form structure and controller logic. Form Modifications for Boolean Values ----------------------- Firstly, we need to alter our HTML structure by adding a value attribute to each checkbox element. The value should be either '1' for checked or leave it empty for unchecked. Now the code will look like this:
{!! Form::open([ 'action' => 'QualityCheckController@validateSave', 'class' => 'quality-check-form', 'method' => 'POST' ]) !!}

        <div class="input-wrpr">
            {!! Form::label('favicon', 'Favicon') !!}
            {!! Form::checkbox('favicon', (old('favicon') == '1' ? '1' : ''), null); !!}
        </div>

        <div class="input-wrpr">
            {!! Form::label('title', 'Page Title') !!}
            {!! Form::checkbox('title', (old('title') == '1' ? '1' : ''), null); !!}
        </div>

        <div class="input-wrpr">
            {!! Form::label('image-optimization', 'Image Optimization') !!}
            {!! Form::checkbox('image-optimization', (old('image-optimization') == '1' ? '1' : ''), null); !!}
        </div>

        {!!  Form::submit('Click Me!') !!}

    {!! Form::close() !!}
Notice that we added a third parameter to the checkbox helper function. This will pass the old value of the respective fields for validation purposes during form submissions. If a checkbox was previously checked, its value will be set to '1' in this parameter, which will be used later by Form requests' validation rules. Form Request Validation and Model Saving ----------------------- To ensure that only boolean values are saved to the database correctly, we need to modify our form request logic. Firstly, add a validation rule for each boolean field within the Form request class:
class CreateArticleRequest extends FormRequest {
    // Other validation rules...

    public function rules() {
        return [
            'favicon' => 'boolean',
            'title' => 'boolean',
            'image-optimization' => 'boolean',
        ];
    }
}
Next, we need to adjust the controller to handle these boolean values properly. Instead of using the create() method on the Article model, use an attribute casting for each boolean field:
public function store(CreateArticleRequest $request) {
    $article = new Article();
    $article->favicon = (bool)$request->get('favicon');
    $article->title = (bool)$request->get('title');
    $article->image-optimization = (bool)$request->get('image-optimization');

    Article::create($article);

    return redirect('articles');
}
In the above code, we've converted each boolean value from a string to its equivalent boolean value before saving it to the database. Conclusion ----------------------- Now that you understand how to efficiently save boolean values in Laravel Eloquent models using checkboxes, you can confidently apply these techniques in your own projects. Remember to always focus on data validation and handling different scenarios (like unchecked or empty boolean fields) to avoid any unexpected results during form submissions or database operations. For more insights into working with forms and models in Laravel, visit https://laravelcompany.com for comprehensive resources, tutorials, and discussions.