How To Add Default Value Of Column In Laravel Migration

Websolutionstuff | May-01-2023 | Categories : Laravel MySQL

In this article, we will explore how to add default values to columns in Laravel 10 migrations, although the information applies to Laravel 8 and 9 as well. Specifically, we will focus on adding default values to columns using Laravel migrations.

Laravel's migration system offers a convenient default() method that allows you to specify the default value for a column. Additionally, you can make use of the nullable() method to set a column as nullable.

Let's dive into the process of adding default values in Laravel 10 migrations, whether you need to set default values for an existing column, work with boolean defaults, or even use the current timestamp as a default. This knowledge is equally applicable to Laravel 8 and 9

Create Migration Command:

php artisan make:migration create_posts_table

Migration:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->text('description')->default('This is test description');
            $table->boolean('is_active')->default(0);
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}


 1. Laravel Migration Default Value Null:

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

 

2. Laravel Migration Default Value Boolean:

$table->boolean('is_active')->default(0);
 

3. Laravel Migration Default Value Current Date:

$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
 

4. Laravel Migration Default Value with Update:

$table->boolean('is_active')->default(0)->change();

 


You might also like:

Recommended Post
Featured Post
Multi-Stage Deployment Pipeline in Laravel 11 with GitHub Actions
Multi-Stage Deployment Pipelin...

In this guide, I'll walk you through setting up a multi-stage CI/CD deployment pipeline for your Laravel 11 project...

Read More

Oct-18-2024

How To Use Sweetalert2 In Laravel
How To Use Sweetalert2 In Lara...

Today we will learn how to use sweetalert2 In laravel, You can use sweetalert2 in laravel as well as php, ...

Read More

May-03-2021

How To Get Element By Data Attribute In jQuery
How To Get Element By Data Att...

In this article, we'll learn how to locate elements using data-attribute values. We can do this using jQuery and CSS...

Read More

Jul-11-2022

How to Search Comma Separated Values in Laravel
How to Search Comma Separated...

Today, in this post i will show you how to search comma separated values in laravel. Here, we will find specific id from...

Read More

Sep-15-2021