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
];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
Please or to participate in this conversation.