How to add a new column to existing table of laravel in a migration

+2 votes

Hii team,

I'm unable to add column to an existing table. I used the below code.

<?php

public function up()
{
    Schema::create('users', function ($table) {
        $table->integer("paid");
    });
}

In terminal, I execute php artisan migrate:install and migrate.

How do I add new columns? Help out please!!

thank you!

Mar 30, 2020 in Laravel by kartik
• 37,520 points
125,160 views

2 answers to this question.

0 votes

Hii @kartik,

To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models

For Laravel 3:

php artisan migrate:make add_paid_to_users

For Laravel 5+:

php artisan make:migration add_paid_to_users_table --table=users

You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}

and don't forget to add the rollback option:

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('paid');
    });
}

:Then you can run your migrations

php artisan migrate

Thank You!

answered Mar 30, 2020 by Niroj
• 82,840 points
Thank you...it worked...i just used your php artisan migrate
Thank you....for the solution

Tried the same methods to modify the existing table (add some new fields and remove some old fields).

After running "php artisan migrate" I got the following errors


   Illuminate\Database\QueryException 

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'confirmed_orders' already exists (SQL: create table `confirmed_orders` (`id` bigint unsigned not null auto_increment primary key, `order_id` int unsigned not null, `final_amount` decimal(8, 2) null, `reason` text null, `updated_by` int null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

What is wrong in this. Why it is not updating the current one instead of trying to creating the new.

thank you, its work
0 votes

You need do little modification in your artisan command

artisan make:migration add_columns_to_users_table

You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this

  public function up()
  {
     Schema::table('users', function($table) {
          $table->type('column');
      });
   }

add the rollback option:

 public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('column');
    });
 }

Then you can run your migrations:

 php artisan migrate
answered Dec 10, 2020 by anonymous
• 82,840 points
To the edureka.co webmaster, Your posts are always informative and up-to-date.

Related Questions In Laravel

0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,840 points
12,372 views
0 votes
1 answer

How to populating a database in a Laravel migration file?

Hello @kartik, Don't put the DB::insert() inside of ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,840 points
3,395 views
0 votes
1 answer

How to change value of a request parameter in laravel?

Hello @kartik, Use merge(): $request->merge([ 'user_id' => ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,840 points
16,966 views
0 votes
1 answer

How to manually create a new empty Eloquent Collection in Laravel?

Hello @kartik, It's not really Eloquent, to add ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
13,831 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,460 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,151 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
3,185 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
45,623 views
0 votes
1 answer

How to select all column name from a table in laravel?

You can get all columns name by ...READ MORE

answered Dec 2, 2020 in Laravel by Niroj
• 82,840 points
6,640 views
0 votes
1 answer

How to get a list of registered route paths in Laravel?

Hello, Route::getRoutes() returns a RouteCollection. On each element, you can ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,840 points
4,054 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP