Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trevorg's avatar

Boolean casting & default value issues

So I have a model like this:

class User extends Model
{
protected $attributes = [
   'is_admin' => 0
];
protected $casts = [
   'is_admin' => 'boolean'
];
}

In my MySQL database, the is_admin column is a TINYINT(1) that does not allow NULL values. (Value should always either be 0 or 1.)

When creating a new model and saving it, I run into this issue:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'is_admin' cannot be null (SQL: insert into `user` (`name`,`is_admin`, `updated_at`, `created_at`) values (Bob,,2015-10-27 20:58:03, 2015-10-27 20:58:03))

So it looks like Laravel is passing an empty string rather than a 0. So, am I storing boolean values incorrectly in the database? Or is something else going on?

0 likes
14 replies
mstnorris's avatar

Why not set a default value in your migration?

$table->boolean('is_admin')->default(false);
// or
$table->tinyInteger('is_admin')->default(0);

And/or:

protected $attributes = [
   'is_admin' => false
];
6 likes
trevorg's avatar

Okay, it turns out the issue was actually with the assignment I was using in creating the model:

$user->is_admin = $request->get('is_admin');

This didn't work because the is_admin value was from a checkbox: <input type="checkbox" name="is_admin" value="1" />. And if the checkbox wasn't passed in the form, then I guess the $request->get('is_admin') returns null, which makes sense.

However, I assumed the model would still cast that value to boolean before saving, but apparently that is not the case. Am I missing something?

2 likes
mstnorris's avatar

Well null is null and without setting a default value, you're not forcing the DB or application to do anything but leave it as NULL.

As I mentioned above, unless they are admin, I would suggest setting a default value of 0 in the database :)

1 like
thomaskim's avatar

In addition to what mstnorris said, you can change that line of code so that it's always set to true or false:

$user->is_admin = $request->has('is_admin');
1 like
trevorg's avatar

Perfect @thomaskim. That should fix it. And yes, the default value is set to 0 in the database. The issue was that line where I used $request->get('is_admin') and it returned a null value.

And I checked and indeed casting does not work if the value is null:

Model.php

   protected function castAttribute($key, $value)
    {
        if (is_null($value)) {
            return $value;
        }
...
3 likes
jekinney's avatar

@trevorgehman it's not passing an empty string, your looking at a null value. If the check box isn't check there is no request for that form input.

Arguably as @mstnorris stated is the preferred method as setting attributes expects there to be a value. Checking and adding a Boolean value is a waste of time imo. Set a default in the database and then your assured a value instead of adding code to do the same thing. You wouldn't buy a two wheel drive truck and add the equipment to make it 4 wheel drive the same day, you would just by a 4 wheel drive truck (hopefully).

It's not like it's a new issue or problem it's something most devs do daily.

MladenJanjetovic's avatar

I have set tinyInteger('something')->default (1); but in database it ends up like 0. It is same for boolan -> true.

Am I doing something wrong?

1 like
Coreation's avatar

@Youndivian did you find a solution, i have the same problem and just fixed it by adding a SQL statement after adding the column, but so far, I assume it's a bug. I'm using L5.3, and whatever I do, default_value(1) or default_value(true), it ends up in the database with default null.

1 like
elsenosy's avatar

@0xAliRaza I have a boolean column active with default value 1, when creating a new item, the response of this field is null, So i solved it using refresh() method

$item= Model::create($data); // default boolean value set to null, active = null
$item->refresh();  // default boolean value set to true, active=1
saliem3651's avatar

Route::get($PageController[2], [RollsController::class , $PageController[2]]);

Hi coder, i want instead of RollsController pass dynamic class.

suppose i have $controller_name = 'RollsController'; // so i want to pass this variable in route instead of RollsController::class , how to achieve them.

Route::get($PageController[2], [$controller_name::class , $PageController[2]]); //i want this type of route with dynamic controller, but it give me error , advance in thanks.

Please or to participate in this conversation.