Learner
Learner

Reputation: 83

How to add multiple columns in already made table in Laravel?

I want to add multiple number of columns in the already made table in Laravel . How can i add multiple columns ?

I don't know how to do add columns in my table. I can only add single column one at a time.

Below given is my migration table up function.

public function up()
{
    Schema::create('matches', function (Blueprint $table) {
        $table->increments('id');
        $table->string('sports');
        $table->string('match');
        $table->string('date');
        $table->string('time');
        $table->string('teamA');
        $table->longtext('teamA_flag');
        $table->string('teamB');
        $table->longtext('teamB_flag');
        $table->string('venue');
        $table->string('status');
        $table->timestamps();
    });
}

This is my table whose name is matches. I want to add two columns using Laravel. The name of columns are: email and qualification.

I am expecting to add multiple number of columns on the table (matches). I want to add multiple number of columns in the already made table in Laravel . How can i add multiple columns ?

I don't know how to do add columns in my table. I can only add single column one at a time.

Upvotes: 8

Views: 18313

Answers (4)

M-Khawar
M-Khawar

Reputation: 940

If you want to add multiple columns to an already existing table you can follow above answer-thread

but In that case, your all columns will be added at the end of already existing columns

so, In order to add multiple columns after a certain column. You can add columns group by using callback/closure as shown in the below snippet:


class AddFieldsInMatches extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('matches', function (Blueprint $table) {

            // closure to add multiple-columns after `id` column
            $table->after('id', function ($table) {
                $table->integer('email')->nullable();
                $table->integer('qualification')->nullable();
            });

        });
    }

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

Note:

  1. Make sure these columns must be nullable otherwise, you will get an error on running php artisan migrate command on the terminal

  2. callback/closure approach is applicable after Laravel 8.27 versions.

Reference Article: click here

Other StackOverFlow threat: click here

Upvotes: 2

Awais Mughal
Awais Mughal

Reputation: 377

You can add new columns by creating a new migration. Remember to make the name of the migration as descriptive as possible.

2021_09_06_083822_update_email_column_and_add_agency_id_and_contact_source_id_in_contacts_table.php

class UpdateEmailColumnAndAddAgencyIdAndContactSourceIdInContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('contacts', function (Blueprint $table) {
            $table->string('email')->nullable();
            $table->integer('agency_id')->after('country_id')->references('agentid')->on('byt_agents');
            $table->foreignId('contact_source_id')->after('agency_id')->nullable()->constrained();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('contacts', function (Blueprint $table) {
            $table->dropColumn(['agency_id', 'email']);
            $table->dropForeign(['contact_source_id']);
        });
    }
}

Upvotes: 2

Emmanuel Coker
Emmanuel Coker

Reputation: 71

You can just run:

php artisan make:migration add_first_item_and_second_item_and_next_item_to_matches

This creates a migration file, you can then add:

 public function up()
{
    Schema::table('products', function (Blueprint $table) {
        $table->string('first_item');
        $table->string('second_item');
        $table->string('next_item');
    });
}

public function down()
{
    Schema::table('products', function (Blueprint $table) {
        $table->dropColumn('first_item');
        $table->dropColumn('second_item');
        $table->dropColumn('next_item');
    });
}

I hope this solves your problem

Upvotes: 7

Regolith
Regolith

Reputation: 2971

Create migration first by php artisan make:migration alter_table_matches, open migration that is created by the command.

public function up()
{
    Schema::table('matches', function (Blueprint $table) {
        $table->string('email')->nullable()->default(null);
        $table->string('qualification')->nullable()->default(null);
    });
}

then in down function

public function down()
{
    Schema::table('matches', function (Blueprint $table) {
        $table->dropColumn('email');
        $table->dropColumn('qualification');
    });
}

Upvotes: 16

Related Questions