ItSolutionStuff.com

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

By Hardik Savani β€’ April 16, 2024
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, laravel 8, laravel 9, laravel 10 and laravel 11. 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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Remove Column from Table in Laravel Migration?

Read Now β†’
β˜…

How to Change Column Name and Data Type in Laravel Migration?

Read Now β†’
β˜…

How to Create Table using Migration in Laravel?

Read Now β†’
β˜…

How to Exclude Route from CSRF Middleware in Laravel?

Read Now β†’
β˜…

Laravel Ajax ConsoleTvs Charts Tutorial

Read Now β†’
β˜…

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now β†’
β˜…

How to Add MySQL Trigger from Migration in Laravel?

Read Now β†’