🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to add a column or columns to an existing table using migration in Laravel.

Laravel Migration

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.....

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.