Laravel Migrations

In this tutorial you will learn about the Laravel Migrations and its application with practical example.

Web application development is an iterative process and it cannot be denied that database schema constantly evolves throughout the course of development. There are situations we may want to add or drop a table, drop or rename a column, drop an index etc. Migrations gives you a way to keep track of the changes we made in database schema during the development process. In collaborative development environment migrations helps you to keep database schema in sync without having to drop and re-create the database each time a change happens.

We can think of migrations as a version control system for database schema, which allows us to keep track of changes made in the database schema. This way it simplifies the development, deployment and updating of any web application.

In Laravel, migrations are simply PHP files stored in /database/migrations directory. Migration files are created over time as we build and modify our application database schema. Each of the migration file contains distinct set of changes to the associated database. In Laravel at any point of time you can switch or migrate to any specific version of the data model.

Artisan Migration Command

Laravel comes with set of migration related commands that can be run from artisan command line interface.

migrate:install
This command is used to create the migration table in the application’s database, this table is used to keep track of migations which have already executed.

migrate
This command is used to run all of the pending migrations for your application.

migrate:make
This command is used to create new migration, the newly created migration is saved in database/migrations directory. A timestamp will be added to the beginning of the migration file name, which allows Laravel to determine the order of the migrations.

migrate:rollback
This command is used to rollback the last migration applied.

migrate:reset
This command is used to rollback all of the migrations applied.

migrate:refresh
This command is used to roll back all of your database migrations, and then re-creates your database schema.

Creating a New Database Migration

Step 1:- In laravel, before creating any migration first thing is to run the following command to create migrations table.

As command executed successfully, you will notice a new table “migrations” is created in database.

Step 2:- Run following command to generate new migration file that will create a new table “posts” in database.

Once the above command executed you will see new migration file is created in the database/migrations directory named as [timestamp]_create_posts_table.php

Structure of a Migration

Let’s open the newly generated migration file, to take a look over the migration structure

Note that every migrations we create extends the Migration class, and must contains two methods “up” and “down”, here –

up() – The up () method includes the set of operation that is executed when the migration is run.

down() – The down () method includes the set of operations that is executed when you rollback migration , it usually reverse the operations performed by the up () method.

In the example, up () method contains a Schema::create function, used to create the “posts” table.

In both of the above methods we can define set operations to create and modify database table using the Laravel schema builder.Laravel’s schema builder provides you the set functions to make any possible database update that you wish to apply.

Please take a look at additional Laravel Schema Builder methods –

Table-level Operations

Laravel Method Purpose
create() Create the table with a name. The second argument is a closure which contains table definitions.
drop() Drops the table, all of its columns and any indexes will also be removed.
dropIfExists() Drops the table if it exists.
rename() Rename the table to a given name.

Column-level Operations

Laravel Method Purpose
dropColumn($columns) It drops the specified column or columns (must be passed as array). Keep in mind that any index associated with that column will also be removed.
renameColumn(‘from’, ‘to’); This command is used to rename the column name

Index-level Operations

Laravel Method Purpose
primary($columns) This command is used to specify the primary key or the composite (must be passed as array)
unique($columns) This command is used to specify a unique index for the table.
index($columns) This command is used to specify an index for the table.
foreign($columns) This command is used to specify a foreign key for the table.
dropPrimary($index) This command is used to drop the given primary key.
dropUnique($index) This command is used to drop given unique key.
dropIndex($index) This command is used to drop the give index.
dropForeign($index) This command is used to drop foreign key.

Step 3:- Finally run the following command to apply the migration to actual database.

In this tutorial we have learn about the Laravel Migrations and its application with practical example. I hope you will like this tutorial.