HDTuto.com

Laravel Migration Add Column to Existing Table Example



Hi Artisan,

In this tutorial, i will show you laravel add new column to existing table migration. i would like to show you add column to table migration laravel. if you want to see example of laravel migration add column to table then you are a right place. I’m going to show you about laravel migration add column.

Here, i will give you full example how to add new column using laravel migration. 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...




April 26, 2020 | Category : Laravel



Blog Search