70

Table 'users':

|id|name|address|post_code|deleted_at|created_at|

and I want add column 'phone_nr' somewhere between 'id' and 'deleted_at'

Is it possible by migrations in Laravel 4.1?

miken32
  • 42,008
  • 16
  • 111
  • 154
Lajdák Marek
  • 2,969
  • 8
  • 29
  • 58

6 Answers6

191

Yes. Create a new migration using php artisan migrate:make update_users_table.

Then use the table command as follows (if you're using MySQL!):

Schema::table('users', function($table)
{
    $table->string('phone_nr')->after('id');
});

Once you've finished your migration, save it and run it using php artisan migrate and your table will be updated.

Documentation: https://laravel.com/docs/4.2/migrations#column-modifiers

yivi
  • 42,438
  • 18
  • 116
  • 138
James Binford
  • 2,753
  • 1
  • 14
  • 12
  • Note that you can change the 'after' value to the column of your choosing. – James Binford Jan 07 '14 at 21:52
  • 6
    What about adding it to the front of the table so that it's the first column? – andrewtweber Dec 13 '14 at 17:13
  • 6
    what if i need to add two columns back to back after specific column. $table->integer('col1')->after('ref_col'); $table->integer('col2')->after('what'); – rahul singh Nov 16 '17 at 06:07
  • You have to change the command to php artisan make:migration update_user_table – Hunter Apr 25 '19 at 16:55
  • @rahulsingh probably not as much use to you nearly 2 years on, but see my answer below https://stackoverflow.com/a/56645714/1372355 regarding your back-to-back new columns query. – Adambean Jun 18 '19 at 09:18
16

The answer by James is still correct. However, Laravel 5 has slightly changed the make migration syntax:

php artisan make:migration add_google_auth_to_users_table --table=users

(I am aware this question is tagged Laravel 4, however it ranks quite high for the question ;) )

Nick
  • 2,803
  • 1
  • 39
  • 59
  • 2
    Had already figured out that the migration command was slightly different, but may come in helpful for others searching for a Laravel 5 solution. As you rightly mention, this ranks quite highly in generic Laravel searches. – Manno Dec 08 '16 at 15:55
16

For anyone else wondering about @rahulsingh's query about adding multiple columns back to back after a single specific column:

$table->integer('col1')->after('ref_col');
$table->integer('col2')->after('what');

You can do this just by writing the new fields in reverse order, for example:

$table->integer('col4')->after('ref_col');
$table->integer('col3')->after('ref_col');
$table->integer('col2')->after('ref_col');
$table->integer('col1')->after('ref_col');

When you run this migration you'll find that your new fields col, col2, ... are in your intended order after the reference field ref_col.

Unfortunately there is no before() function for adding fields before an existing field. (If that existed we could keep the above statements in their real order.)


You cannot do chaining with fields that do not yet exist, for example:

$table->integer('col1')->after('ref_col');
$table->integer('col2')->after('col1');
$table->integer('col3')->after('col2');
$table->integer('col4')->after('col3');

This is because the migration blueprint is compiled to a single query thus executed as one, so when it comes to adding col2 your database engine will complain that col1 doesn't exist.

Adambean
  • 1,096
  • 1
  • 9
  • 18
11

If you need add several columns after specific column in laravel 8:

When using the MySQL database, the after method may be used to add columns after an existing column in the schema:

$table->after('password', function ($table) {
    $table->string('address_line1');
    $table->string('address_line2');
    $table->string('city');
});

https://laravel.com/docs/8.x/migrations#column-order

DeN
  • 105
  • 1
  • 4
1

As of Laravel 5.1, you can also position a new (mysql) column first. This was tested with Laravel 6:

Schema::table('foo', function (Blueprint $table) {
    $table->increments('id')->first();
});
oriberu
  • 1,186
  • 9
  • 6
-2

For latest versions of Laravel say 5-6

The command is

php artisan make:migration update_users_table

Then

php artisan migrate
Shaan
  • 475
  • 3
  • 20