We’ll be creating plenty of database migrations throughout the course, so it’s good to get some extra practice working with them.
Refer to the Laravel Migrations documentation for assistance with creating, running, and writing migrations.
In this exercise, you’re going to create, and rollback, a test migration.
1. Create a test migration
In the command line first make sure that:
- you’re connected to your virtual machine; and
- you’re inside of your application’s main directory.
Then run the command to generate a new migration file to add a new test
column to your [objects]
database table.
Note, you aren’t creating a new table this time, so don’t use the --create
option.
And give the migration a nice, readable name, like add_test_to_[objects]_table
.
BONUS POINTS: Use the --add
option when you generate the migration to specify the name of your table.
2. Write the migration file
Open up the generated migration file in Sublime Text. Remember, migration files are located in database/migrations
.
Now you need to fill in the contents of the up
and down
functions.
In the up
function, add code to add the new test
column to your [objects]
table.
And in the down
function, add code to drop the test
column from your [objects]
table.
Make sure you save the file, and then move on!
3. Run the migration to add the new column
That’s it. Just run the migration from the command line. One command.
After it runs, log into phpMyAdmin to confirm that column was successfully added.
Is it there? Good. Now let’s get rid of it :)
4. Rollback the migration to remove the column
There’s just one command required to rollback the migration and remove that pesky test
column.
Run the rollback command and then check phpMyAdmin again to make sure the column was removed.
Gone? Bravo.