Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 631
Learner Avatar asked May 26 '19 08:05

Learner


2 Answers

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');
    });
}
like image 156
Regolith Avatar answered Oct 04 '22 02:10

Regolith


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

like image 30
Emmanuel Coker Avatar answered Oct 04 '22 03:10

Emmanuel Coker