Skip to content

Instantly share code, notes, and snippets.

@deepak-cotocus
Last active July 7, 2020 12:03
Show Gist options
  • Save deepak-cotocus/1b0793225374185e609acfe13b79e66e to your computer and use it in GitHub Desktop.
Save deepak-cotocus/1b0793225374185e609acfe13b79e66e to your computer and use it in GitHub Desktop.
How to add a column to an existing table using migration in laravel.

Steps to add a column or columns to an existing table using migration in laravel.

  • Laravel Migration Example Tutorial. This tutorial explain you step by step, how you can add single or multiple columns in your existing database tables.

  • Sometimes there is a need to add new columns in your database tables. Today we will show you how to add new columns or columns to an existing table using Laravel migration.

Let’s have a table called Schools where the table you want to add phone_number type.

Add a single Column an existing table

STEP 1:

Add column name into the school.php model.

addColumn2

STEP 2:

To add a new column to the existing table using the laravel migration. Let’s open your terminal and create a migration file using the below command:

php artisan make:migration add_phone_number_to_schools

add_column1

It will create an add_type_to_notes file in the migration folder. it will look like this-

image

STEP 3:

Add below code into newly added migration file.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddPhoneNumberToSchools extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::table('schools', function (Blueprint $table) {
            $table->string('phone_number', 10)->nullable(false)->default(null);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('schools', function (Blueprint $table) {
            $table->dropColumn('phone_number');
        });
    }
}

STEP 4:

Run Migration Command

--Initially schools table in db looks like this: image

Run the below-given command in your terminal. It will add a new column to your table.

After running below command phone_number column will reflect. php artisan migrate image

--Now, schools table in db looks like this: image


Add Multiple Columns in Existing Table

STEP 1:

If you need to add multiple columns, you need to add the following command in the terminal.

php artisan make:migration add_multiple_column_to_schools

image

STEP 2:

Add multiple column names like afiliated_by, short_description, medium in your School.php model.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class School extends Model
{
    protected $fillable = [
        'school_name', 'email', 'country', 'phone_number', 'Afiliated_by', 'School_location', 'Medium'
    ];
}

STEP 3:

Add below code into newly added migration file

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddMultipleColumnToSchools extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('schools', function (Blueprint $table) {
            $table->string('afiliated_by')->nullable();
            $table->string('short_description')->nullable();
           $table->string('medium')->nullable()
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('schools', function (Blueprint $table) {
            $table->dropColumn(['afiliated_by',  'short_description', 'medium']);
        });
    }
}

STEP 3:

Run below Command and Check database:

php artisan migrate image

** Check whether multiple Column has been added succesfully.. ** image


Referance:

Watch a Vidio on above topic


Thanks.....

@deepak-cotocus
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment