A Laravel migration is stored in your application’s app/database/migrations folder. Each file deal with some set of chnages for the corresponding databases. Changes to the database are made in PHP rather than a database-specific flavour of SQL. Your PHP migration code ends up being converted into the DDL specific to your current database; this makes switching database platforms very easy. Since migrations are kept in their own directory, it’s pragmatic to include them into version control just like any other project code. Laravel migrations are run explicitly from command-line using the artisan tool.

Now we have to deal with migration:
We are going to use Laravel’s command line tool artisan to create our new migration. To run artisan you will need to open a terminal to the root of your Laravel application folder where the artisan script exists. The first thing we need to install the migration table, so that Laravel can keep track of which migrations have been run. Run below command to create the special table in database.

If success, artisan will respond with a message “Now we’re ready to do some migrating!” and you can see database table “migrations”. You don’t need to be overly concerned about the table. This is simply a table that helps Laravel to keep tabs on the migrations that you have and have not run. When you add new migrations, artisan migrate will check the migrationstable and execute all migrations that have not yet run.

Now, let’s create an actual migration file. We’d like to create a new table named “authors”. Let’s run below command in terminal.

Now you can see this message “Migration created successfully!”

We are telling artisan to run the make method on the migrate task and we pass an underscored delimited list of words which describe our migration in human readable terms. You may name the migration as you wish, but you may want to give it a name that describes exactly what you’re doing. Laravel does not care what the migration is called, it is just for your sanity. In this case we are creating the “authors” table, therefore we’ve named our migration “create_authors_table”.

If you run above command you’ll see that a new migration has been added. Here’s what the app/database/migrations directory of looks like:

then migration file automatically create like this 2012_12_25_161424_create_authors_table.php

As you can see, we now have a new migration file whose name contains time-stamp as well as the descriptive name of our migration. The general structure of a migration file look like this:

With Laravel you can migrate to specific versions of the data model at any point in time. The code in the up() method is executed when migrating forward while down() is executed when migrating backward (that is, creating a new version of the database or rolling back to a previous version). An easy way to think about it is that up() is the action you want to perform in the migration file, and down() is the exact opposite. So, for example, if you want to create a table called ‘authors’, you create it in the up() method and then destroy it in down().

Creating A Seeder
Laravel also includes seeding system, which can be used to add records to your database after initial migration. To add the initial users to our project following seeder class required.

Save this file app/database/seeds/UserSeeder.php Run this file to add user account information to the database, but in order to run this; we need to call seeder from main DatabaseSeeder class like below.

This file should be saved as app/database/seeds/DatabaseSeeder.php The DatabaseSeeder class seeds the users table with our account when invoked. If you’ve already set up your migration and model, and provided valid database connection details, then the following commands should get everything up and running.

Generating autoload files:

Migrated: xxxx_xx_xx_xxxxxx_create_user_table

Seeded: UserSeeder

The first command makes sure all the new classes we’ve created are picked up by the class autoloader. The second creates the database tables specified for the migration. The third seeds the user data into the users table.

Enjoy database migration!