How to Add Boolean Column in Laravel Migration?
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Migrate SQL File in Laravel Migration?
- Laravel Migration Execute SQL Query Example
- Laravel Migration Remove Default Value Example
- Laravel Migration Change Datatype Timestamp to Datetime Example
- Laravel Migration Change Datatype Date to Datetime Example
- Laravel Migration Change Default Value Example
- How to Drop Soft Delete from Table using Laravel Migration?
- Laravel Migration Default Value Current Timestamp Example
- Laravel Migration Enum Default Value Example
- Laravel Migration Add Enum Column Example
- How to Rollback Migration in Laravel?
- How to add Default Value of Column in Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- How to Create Table using Migration in Laravel?