Spread the love

To set default null value in laravel migration we use nullable method of schema class. As we learnt about How to Add Default Value to Column in Laravel Migration? in our last article but sometimes we want to set null value to column instead of any number or string. Null value is useful when we want to auto fill the default null if there is no assignment to column so we can handle the relations and data type accordingly.

So In our this article i will show you to set value null to column in laravel migration. We can add any type of data type and configuration to table and column using laravel migration therefore we also want to set default value null to column so its inserted by null value if it not defined by the user.

In this post we will take a simple example in which we will create a table and column with default null value. this example will work all version of laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.

creating default null value field using nullable method of laravel Schema class

Here is the syntax

$table->string('status')->nullable();

We will use the php artisan command to generate the Laravel migration and php artisan migrate command to modify the table.

Let’s understand it step by step

Step 1 : Generate migration file

To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command

php artisan make:migration movies_table

Above command will create a migration file in folder database/migrations

Output: 
Created Migration: 2022_06_30_174050_movies_table

Step 2 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to add a column to the table so adding a status column as string with null value.

so let’s open the file and start editing

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->id();
            $table->string('email')->unique();
            $table->int('user_id');
            $table->string('title');
            $table->string('body')->nullable();
            $table->string('status')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::dropIfExists('movies');
    }
}

as you can see we have created string field using $table->string('status')->nullable(); syntax.

Step 3 : Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

This will create table in database and the output

Output: 
Migrating: 2022_02_15_174050_movies_table
Migrated:  2022_02_15_174050_movies_table (20.08ms)

Also Read : How to delete column in laravel migration ?

Leave a Reply