Skip to content
Advertisement

Laravel Add a new column to existing table in a migration

I can’t figure out how to add a new column to my existing database table using the Laravel framework.

I tried to edit the migration file using…

JavaScript

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

How do I add new columns?

Advertisement

Answer

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 5+:

JavaScript

for Laravel 3:

JavaScript

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:

JavaScript

and don’t forget to add the rollback option:

JavaScript

Then you can run your migrations:

JavaScript

This is all well covered in the documentation for both Laravel 4 / Laravel 5:

And for Laravel 3:

Edit:

use $table->integer('paid')->after('whichever_column'); to add this field after specific column.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement