If you are not set default value in your table column and if you do not provide data for that column when you will save into database, then you will face errors like field doesnt have a default value laravel 9. So in this tutorial, I will show you how to set default value in laravel migration. 

Let's see the example code of laravel migration 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.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->text('body')->default('lorem ipsum donor');
            $table->boolean('status')->default(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
};

 

Laravel migration default value null example:

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

 

Laravel migration default value boolean example:

$table->boolean('status')->default(false);

 

Laravel migration default value current date example:

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

 

Read also: How To Add Boolean Column In Laravel Migration?

 

Conclusion

I have tried to discuss the clear concept of Laravel migration default value current date example. Now we know set default value in laravel migration. Hope this default value in laravel migration tutorial will help you.