Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migration add field after data is in table?

I have a migration called CreateItemsTable; I ran that, I have items in that table, now I need to add a new field to the table. I can't just add a field to the migration file and migrate:refresh because I need the data that's in it.

Am I supposed to make another migration for adding a field? That's seems like mess while I'm testing things in development, I might change fields a lot. I'm not sure if migrations are cleaner than just PhpMyAdmin... or maybe I don't understand them?

like image 216
Farzher Avatar asked Apr 20 '13 16:04

Farzher


1 Answers

Yes, each time you need to change a table in some way you'd create a new migration for it. That's the whole point of migrations. When you're developing in a collaborative environment and you pull down some changes from a remote repository, one of the things you should do (if working with a database) is run any migrations that other developers might have created. This keeps your databases in sync.

Sure you might drop and add columns occasionally but it's no big deal.

When you create a table for the first time you are probably using Schema::create(). All subsequent migrations for that table should use Scheme::table(). It accepts the same parameters except it doesn't attempt to create the table first.

like image 170
Jason Lewis Avatar answered Sep 28 '22 19:09

Jason Lewis