ItSolutionStuff.com

Laravel Migration Remove Default Value Example

By Hardik Savani • November 5, 2023
Laravel

Hello Folks,

This detailed guide will cover laravel migration remove default value. we will help you to give an example of how to remove default value in laravel migration. This post will give you a simple example of laravel remove default value migration. This tutorial will give you a simple example of laravel migration remove column default value.

Laravel migration provides way to add column name and datatype with default value '0' or nullable. But if you need to change column default value then you have to install doctrine/dbal package to change default value. In this example i will show you two ways to change column default value in laravel migration.

In this example, i will add publish_date column timestamp with nullable, then i will remove default value null from publish_date column.

So, let's see the simple example of laravel migration change default value.

Install doctrine/dbal: optional

First of all we need to install "doctrine/dbal" composer package. This package allow to use change() method to update datatype using laravel migration.

composer require doctrine/dbal

Default Created Table

Here, you will see the default created table screenshot.

Way 1: Create Migration

Create new migration using following command:

php artisan make:migration change_datatype_column

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

Schema::table('posts', function (Blueprint $table) {

$table->timestamp('publish_date')->nullable(false)->change();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::table('posts', function (Blueprint $table) {

});

}

};

Now, you are ready to run migration command:

php artisan migrate

Way 2: Create Migration

Create new migration using following command:

php artisan make:migration change_datatype_column

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

\DB::statement('ALTER TABLE `posts` CHANGE `publish_date` `publish_date` TIMESTAMP NOT NULL;');

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::table('posts', function (Blueprint $table) {

});

}

};

Now, you are ready to run migration command:

php artisan migrate

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Migration Change Datatype Date to Datetime Example

Read Now →

Laravel Migration Change Default Value Example

Read Now →

Laravel Migration Change Datatype Date to Timestamp Example

Read Now →

Laravel Migration Change Datatype Integer to Decimal Example

Read Now →

Laravel Migration Change Datatype Int to Bigint Example

Read Now →

Laravel Migration Change Datatype Integer to String Example

Read Now →

Laravel Migration Change Datatype String to Integer Example

Read Now →

Laravel Migration Change Datatype Varchar to Text Example

Read Now →

Laravel Migration Change Datatype Text to Longtext Example

Read Now →

Laravel Artisan Command to Create Migration and Model Example

Read Now →

Laravel Migration Add Column After Column Example

Read Now →

Laravel Migration Default Value Current Timestamp Example

Read Now →

Laravel Migration Enum Default Value Example

Read Now →

Laravel Migration Custom Index Name Example

Read Now →

Laravel Migration Custom Foreign Key Name Example

Read Now →