ItSolutionStuff.com

How to Add Boolean Column in Laravel Migration?

By Hardik Savani • November 5, 2023
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...

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

How to Migrate SQL File in Laravel Migration?

Read Now →

Laravel Migration Execute SQL Query Example

Read Now →

Laravel Migration Remove Default Value Example

Read Now →

Laravel Migration Change Datatype Timestamp to Datetime Example

Read Now →

Laravel Migration Change Datatype Date to Datetime Example

Read Now →

Laravel Migration Change Default Value Example

Read Now →

How to Drop Soft Delete from Table using Laravel Migration?

Read Now →

Laravel Migration Default Value Current Timestamp Example

Read Now →

Laravel Migration Enum Default Value Example

Read Now →

Laravel Migration Add Enum Column Example

Read Now →

How to Rollback Migration in Laravel?

Read Now →

How to add Default Value of Column in Laravel Migration?

Read Now →

How to Remove Column from Table in Laravel Migration?

Read Now →

How to Change Column Name and Data Type in Laravel Migration?

Read Now →

How to Create Table using Migration in Laravel?

Read Now →