Ronnie Ronnie - 2 months ago 10x
PHP Question

Adding a column to existing table

I had a migration called

that ran

Schema::create('projects', function(Blueprint $table) {

I needed to add a new column called
. I assumed I could just add


and run
php artisan migrate
, but I was wrong, didn't work. I then read and learned I have to use
so I changed my existing migration (projects_table) to:

Schema::table('projects', function(Blueprint $table) {

and that still did not work. I finally got it to work by creating a new migration
and simply adding

Schema::table('projects', function(Blueprint $table) {

My question is, is that the right way to do this in laravel? Adding a column is a pretty basic task and If I have to create a migration for every database change, I think that migration folder will get long quick. What is the right way to do this?

edit: I also see it is important to have a
field for migration purposes. I will add those fields as well


Yes, creating a new migration when you want to update the structure of the table is the right thing to do.

It allows you to "push" this modifications on a live server without breaking the existing database.

Their's another way of doing it, but it might no be applicable if you already stored datas in your tables :

  1. Rolling back migrations using php artisan migrate:reset
  2. Updating your existing migration file and add the new field in the field list
  3. Running migrations again : php artisan migrate

Keep in mind that this is going to delete all your existing tables, ending up in a loss of datas in case you already stored some.