Tips & Tricks about Laravel Migration

How to Create Migrations in Laravel

You can create a migration file using the Laravel make migration command. You need to specify a meaningful filename like below:

If you are creating migration to add a new table, you should pass --create parameter along with table name. This will add some Boilerplate code to both up() and down().

If you are looking to modify a table, you should consider using --table parameter to add some Boilerplate code.

Pro Tip: You can simultaneously create a migration when you are creating a Model using make:model by passing the -m flag.

Running Migrations

You can run migration using the following command

It will run all the pending migrations in your application.

Hidden Feature: If you are not sure what changes will be run when you run the migration, you can use the –pretend flag. This will show all the queries that will be run when you run migration, but it won’t actually run them. This is helpful when you are not sure about the code that you have written in migration.

Reverting a Database Change

If you want to rollback last migration, you can do so by running the following command:

It will revert the latest migration. If you want to revert the last 5 migrations, you can do so by passing the –step flag

If you want to revert all the migrations, you can do so using below command

Sometimes you need to rerun all your migrations. In this case you can do so by using the following commands

Alternatively you can use below command which is equivalent of above 2 commands:

Table Commands

Before adding a Table using migration, you can check if a table exists using.

You can rename a column, using below command:

To drop a column, you could use

You can specify the type of the table using following command:

Column Commands

You can specify a default value of the column using the default()

In order to make sure that column can have null value, you could use nullable()

In order to define an index on the column, you could use index()

You could also define a unique index using below command

In order to change a column type, you could define the new column attribute and then call change()

To rename a column, you could use renameColumn method

Similarly to drop a column, we can use dropColumn()

To add a column after specific column, you could use after() method

This will add the column type after name column. By default, column is always added at last.

You can also add the column as the first column of the table using first()

Now type column will be added as the first column of the table.

Similar to checking if a table exists, you can also check if a column exists using

How to delete a Migration.

Short Answer: No, Don’t Delete.

Long Answer: You should never need to delete a Migration. Suppose you created a companies table in say Revision 6. Now you are at Revision 10. And now suppose you no longer need this Table along with the data, so you feel the need to Delete the Migration 6. However, instead of Deleting the Migration, what you need to do is create a New Migration and drop the Table in this New Migration. Same solution would apply in the case if you had changed the column type and wanted to revert that later on.

However under some circumstances during development when you need to delete your last Migration, you should first rollback to the version prior to the one you want to delete. Then you should delete the particular Migration File. And then finally you should run the migrate command again.

Freelancer Developer.