How to Add Boolean Column in Laravel Migration?

By Hardik Savani November 5, 2023 Category : Laravel

Hey pals,

In this article, I will be showing you how to add a boolean column in Laravel migration. You will also get to learn about setting a default value for a boolean column in Laravel migration, wherein I will be using "false" as the default value. I will explain the concept of boolean values, i.e., true and false, in a simple manner.

Laravel Migration provides boolean() method to add boolean column in laravel. we can also use default() method to set default true/false value in column as well.

So, let's see the simple example of it.

Laravel Migration with Boolean Column:

<?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::create('posts', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->boolean('status');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('posts');

}

};

Laravel Migration with Boolean Column(Default Value):

<?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::create('posts', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->boolean('status')->default(false);

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('posts');

}

};

Now, you can check your own.

I hope it can help you...

Shares