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

https://www.itsolutionstuff.com/post/how-to-add-new-column-in-existing-table-in-laravel-migrationexample.html

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180539_create_posts_table.php

<?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');
      $table->text('body');
      $table->boolean('is_publish')->default(0);
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('posts');
  }
}

C:\xampp\htdocs\reset\database\migrations\2022_05_20_180718_change_posts_table_column.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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() {
    //
  }
}

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

By Hardik Savani April 1, 2020 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenVDO.AIHi 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 and laravel 9. 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:

Read Also: How to Change Table Name using Laravel 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...

Last updated