Laravel Migration Add Default Value Example

Laravel Migration Add Default Value Example

In this example you learn laravel migration add default value example. I explain step by step how to add default value of column in laravel migration. In this post we will discuss on laravel migration set default value of column.  I will show set default value of table column using migration in laravel

This post you give laravel migration default column value example. laravel migration provide default() and nullable() methods where you can set default value of that column. We will show example of laravel database migration default value.

Read Also :  How to Get Current User Location in Laravel

Set Default Value Laravel Migration

Create Laravel Migration Command:

php artisan make:migration create_products_table
<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->text('description')->default('NO BODY');
            $table->decimal('price',8,2)->default(0);
            $table->boolean('is_active')->default(0);
            $table->enum('deleted',[1,2])->default(1);
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

1) Laravel Migration Add Default Value Null:

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

2) Laravel Migration Add Default Value Boolean:

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

3) Laravel Migration Add Default Value Current Date:

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

4) Laravel Migration Default Value with Update:

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

I hope you understand of laravel migration default value and it can help you…

Read Also :  Laravel Collect Helper Methods Example