Hi Dev,
Today, i we will show you how to add laravel 8 migration default value of column. This article will give you simple example of how to add laravel 8 migration default value of column. you will learn how to add laravel 8 migration default value of column.
laravel 8 migration provide default() and nullable() where you can set default value of that column. i will give you simple example how to add default value as null, boolean, current time etc. you simple set with laravel 6, laravel 7 and laravel 8 version.
So let’s follow few step to create example of how to add laravel 8 migration default value of column.
Create Migration
php artisan make:migration create_items_table
database/migrations/2021_04_07_125911_create_items_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateItemsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('items', function (Blueprint $table) { $table->id(); $table->string('title')->nullable(); $table->text('body')->default('NO BODY'); $table->boolean('is_active')->default(0); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('items'); } }
Step 1:- Laravel 8 Migration Default Value Null
$table->string('title')->nullable();
Step 2:- Laravel 8 Migration Default Value Boolean
$table->boolean('displayed')->default(0);
Step 3:- Laravel Migration Default Value Current Date
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
Step 4:- Laravel Migration Default Value with Update
$table->boolean('displayed')->default(0)->change();