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
123,895 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
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

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,103 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,113 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,640 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,469 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,017 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
2,999 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
2,922 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
44,723 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,457 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
3,784 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