In this article, we will see how to add the default value of a column in laravel 10 migration. Here, we will learn about laravel 10 adding the default value of a column using the migration.
Laravel migration provides a default() method where you can set the default value of that column. Also, you can set the null value using the nullable() method.
So, let's see laravel 10 migration add a default value to an existing column, set default value null in laravel 10 migration, laravel 10 migration default value boolean, laravel 10 migration default value current timestamp, how to add the default value of a column in laravel 8, laravel 9, and laravel 10.
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: