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:

php artisan make:migration create_companies_table 

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().

php artisan make:migration create_companies_table --create==companies

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

php artisan make:migration modify_companies_table --table==companies

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

php artisan make:model Company -m

Running Migrations

You can run migration using the following command

php artisan migrate

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

php artisan migrate --pretend

Reverting a Database Change

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

php artisan migrate:rollback

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

php artisan migrate:rollback --step=5

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

php artisan migrate:reset

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

php artisan migrate:reset
php artisan migrate

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

php artisan migrate:fresh

Table Commands

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

	if (Schema::hasTable('users')) {
	    //Create Table code goes here.
	}

You can rename a column, using below command:

Schema::rename('old_table_name', 'new_table_name);

To drop a column, you could use

Schema::dropIfExists('companies');

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

$table->engine = 'InnoDB';

Column Commands

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

$table->string('type')->default('Default Value');

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

$table->string('type')->nullable()

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

$table->integer('user_id')->index()

You could also define a unique index using below command

$table->string('name')->unique();

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

$table->string('name', 50)->change()

To rename a column, you could use renameColumn method

$table->renameColumn('old_column', 'new_column');

Similarly to drop a column, we can use dropColumn

$table->dropColumn('column_name')

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

$table->string('type')->nullable()->after('name');

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()

$table->string('type')->nullable()->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

	if (Schema::hasColumn('type')) {
	    //Create Column code goes here.
	}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *