Pages

Laravel: Adding new column/columns to the existing table in a migration

Adding New Column to an existing table

In this article, you will learn the step-by-step procedure to add single or multiple columns in our existing database tables using Laravel migration.


Step 1: To get started, open the command prompt or terminal and navigate to the root directory of our Laravel project.



Step 2: Type the following command to create a new migration file:


php artisan make:migration add_phone_number_to_schools


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



Now, replace the column_name with the name of the column you want to add and the table_name with the name of the table you want to modify.


Step 3: Open the newly created migration file in the database/migrations folder. Inside the up function, add the code to create the new column or columns:


<?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 the migration command.


Initially, our screen will look like the below image. 


As soon as we run the migration command a new column will be added and our database will look like the below image.



To make our database look like the above image and add a new column, we have to add the below code to our database.


php artisan migrate

C:\xampp\htdocs\laraveltest>php artisan migrate

Migrating: 2023_05_01_055549_add_phone_number_to_schools

Migrated: 2023_05_01_055549_add_phone_number_to_schools (21.14ms)

C:\xampp\htdocs\laraveltest>


Adding Multiple Columns to an Existing Table using Laravel Migration

To add multiple columns to the existing table 


Step 1: Navigate to the root directory and enter the following command

php artisan make:migration add_multiple_column_to_schools


Step 2: As we want to add multiple columns to the existing table, we have to introduce the column name in our code. 


Use the below command to introduce new column names. 


<?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: Adding code to the 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 4: Run the below command and check the database:


php artisan migrate



The final image shows that we have successfully added multiple columns in the existing table using Laravel migration.


No comments:

Post a Comment

Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...