Laravel migration : An Introduction

Laravel migration

Laravel migration provides a method for creating the database and it is possible to modify the database. It is one of the most required ones. In Laravel migration you need not worry about the specific SQL syntax, that is creating tables for the database engine. Migration helps you to take back the most recent changes that you have made to a database.

How to generate Migrations in Laravel

make: migration command was used to create migration.(An artisan command)

php artisan make:migration create_users_table

Database/migration directory will contain the new migration and it is placed in it. In order to determine the migration, each and every file in-migration will contain the timestamp which permits Laravel to analyze the order of migration.

To indicate the table name and whether the new table is being created by migration -table and -create option can be used. The generated will gets pre-filled by the stub file.

php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=users

You can also able to make a custom output path for the migration which is generated.To do this task you need to use the path option while the time of executing the command that is make:migration.The base path of the application should somewhat be related the given path.

Laravel Structure of Migration

The class of Migration is having two methods, namely up and down.With the help of up method, you can add tables, rows, columns or database indexes, while the down allows the exact opposite operations that are performed by up.It is similar to the ” undo “.

Laravel schema builder was used to perform the above two methods, which is mainly for the modification and creation of tables.


<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_profiles', function (Blueprint $table) {
            $table->increments('id');
			$table->integer('user_id');
            $table->string('location');
			$table->text('bio');
			$table->string('website');
			$table->string('profile_image');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_profiles');
    }
}

Running Migrations in Laravel

All of your outstanding migration can be run through execution of migrate artisan command.

php artisan migrate

Note :

You should run the command only in Virtual Machine, In case if you use Homestead Virtual Machine.

Some of the operations in migration may cause destruction, in other words, there is also a possibility for you to loose your data. To protect and prevent these command execution against the production database, you will get confirmation in the prompt message before command gets to execute.

Force flag can be used to run commands without a prompt.

php artisan migrate --force

How to roll back migration in Laravel?

The rollback command may be used to undo the recent operation performed.

php artisan migrate:rollback

If you need to roll back a particular number of steps, the following command may be executed.The following command will roll back recent 4 steps.

php artisan migrate:rollback –step=4

The following command will rollback the entire migration.

php artisan migrate:reset

Conclusion

I hope this article will surely help you to know some information about the migration in Laravel framework.To more about this topics keep us updated.

thank you !!

Leave a Reply

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