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
10 Powerful React Native Tools for 2024
10 Powerful React Native Tools...

React Native is popular among businesses because of its speed, efficiency, and lower costs. Developers also appreciate i...

Read More

Sep-06-2023

How To Remove index.php From URL In Laravel 9
How To Remove index.php From U...

If you're a developer, you're likely to have frustration with "index.php" cluttering up your website&#...

Read More

Jan-13-2023

How To Get Current Date And Time In Node.js
How To Get Current Date And Ti...

In this example we will see how to get current date and time in Node.js application. In Node.js date and time are handle...

Read More

Sep-01-2021

How to Install PHP PDO MySQL Extension in Ubuntu 22.04
How to Install PHP PDO MySQL E...

Hey there! If you're diving into PHP development on Ubuntu 22.04 and need to connect your applications to MySQL data...

Read More

Feb-28-2024