Set default to NULL with laravel migration
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Setting Defaults for NULL Values in Laravel Migrations
Introduction:
In developing web applications using Laravel, we frequently encounter the need to define defaults for columns in database tables. In this post, we will address a common query regarding setting default values for fields that can be NULL in a migration. We'll show you how to correctly assign a NULL value as its default while avoiding unwanted string representations of NULL.
Body:
Let's first dissect the code provided in the example. The user has attempted to set the `parent_item_category_id` field to be unsigned, nullable, and have a custom default value. In this case, they've used
"NULL" as the default, which is not the correct approach. This will lead to an unexpected result of storing the string "NULL" instead of setting it to NULL.
The right way to set defaults for NULL fields in Laravel migrations:
1. Understand the available types and their behaviors:
- `nullable()`: Defines that a column may contain NULL values while accepting other values as well.
- `default()`: Sets a default value for the column if no specific value is provided during insertion or update operations.
- Default values can be strings, integers, floats, booleans, arrays, dates, and even closures.
- To ensure proper data types are applied, Laravel provides methods like `unsigned()`, `integer()`, etc., to define column types.
2. Use the correct default value:
- For NULL values, always use the appropriate option for the type you've defined:
- `nullable(NULL)` when defined as nullable but with no specific default (e.g., $table->integer('column')->unsigned()->nullable(NULL);).
- `default(NULL)` when defining a field that defaults to NULL if not specified in the data (e.g., $table->integer('column')->nullable()->default(NULL);).
- For other types, use the appropriate default value based on their type:
- Integers: Use an integer literal or string representation of an integer value (if necessary to indicate the base).
- Floats: Use a floating-point literal or its string representation.
- Arrays and objects: Use serialized data in JSON format, with appropriate keys for arrays and properties for objects.
Code examples:
1. Setting a NULL default value for an integer column (unsigned) with a custom migration:
`Schema::create('users', function(Blueprint $table){
$table->integer('age')->nullable()->default(NULL);
});`
2. Using a different default value than NULL for other field types:
`$table->string('name')->nullable()->default('Unnamed User');`
3. Setting defaults for columns with composite migration:
```php
Schema::create('posts', function(Blueprint $table){
$table->integer('comments_count')->unsigned()->nullable()->default(0);
});
Schema::create('comments', function(Blueprint $table){
$table->foreignIdFor('post')->constrained();
});
```
Conclusion:
Setting the correct default value for a column in Laravel is crucial to ensure its initial state and provide expected results. To achieve this, use `nullable()`, `default()`, and define appropriate types and values based on your requirements. This will help you avoid unexpected behavior, and enable your application to function as intended. Remember that the best practice should always be to follow Laravel's documentation for creating and managing database schemas.