Laravel Migration - How to Add New Column in Existing Table ?

By Hardik Savani April 1, 2020 Category : Laravel

Hi Artisan,

Today, laravel migration add column to existing table is our main topic. let’s discuss about laravel add new column to existing table migration. you will learn add column to table migration laravel. you'll learn laravel migration add column to table.

Here, i will give you full example how to add new column using migration in laravel 6, laravel 7 and laravel 8. we will add new column on existing table. so let's see bellow full example step by step.

Migration for main table:

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('posts', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('title');

$table->text('body');

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

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

}

}

Add Column using Migration:

<?php

use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class ChangePostsTableColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('posts', function (Blueprint $table) {

$table->integer('auther_id');

$table->string('note');

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

I hope it can help you...

Tags :