Laravel Migration

Introduction

  • Laravel migrations provide mechanisms for creating and modifying database tables.
  • Migrations also allow you to roll back the most recent changes that you made to a database.

Generate Migrations

  • To create a migration, use the make:migration Artisan command:
  • When you create a migration file, Laravel stores it in /database/migrations directory.
  • Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
  • Open the command prompt or terminal depending on your operating system.

Create Migration Table

Run the following command to create a migration file: php artisan make:migration create_yourtablename_table

Here,

  • Php artisan make:migration executes the make migration method via the artisan command.
  • You will get following results for your migration file.

Create Migration: 2016_09_15_2434_create_yourtablename_table

Migration Structure

We will now examine the contents of the created migration file Open the file.

/database/migrations/2016_09_15_2434_create_yourtablename_table

Migration Structure

Here,

  • Class CreateYourtablenameTable extends Migration defines the CreateYourtablenameTable class that extends Migration class.
  • Public function up() defines the function that is executed when the migration run.
  • Public function down() defines the function that is executed when the migration rollback.

How to create a table using Migration

  • Now that we have successfully created migration file for yourtablename.
  • We will add the table definition fields in the Migration file.
How to create a table using Migration

Here Explain Migration,

  • Schema::create(‘yourtablename’, function (Blueprint $table) {…} calls the create function of the Schema class. The create function is responsible for creating the database table.
  • $table->increments(‘id’); increments are used to define an auto increment field.
  • $table->string(‘yourname’,60); string is used to define varchar fields. The second parameter is the length of the field.
  • $table->text(‘youraddress’); is used to define text fields.
  • $table->integer(‘pincode’); integer is used to define int fields.
  • $table->date(‘birthdate’); is used to define date fields.
  • $table->timestamps(); is used to automatically create two time stamp fields namely created_at and updated_at.

Go back to the command prompt Or terminal Run the following command for run migration file.

php artisan migrate

You will get the following output

Php Artisan Migrate

Laravel Migration Rollback

  • One of the most advantages of migrations is that they allow you to roll back to the previous state before you run the migrations.
  • In this section, we will roll back the creation of the tables.
  1. Go back to the command prompt
  2. Run the following command

php artisan migrate:rollback

You will get the following output.

Laravel Migration Rollback

Hope this would help you and make Laravel Migration easy going. Drop us the message if you have any queries/concern regarding Laravel.

We are readily available to resolve it. #letstalksolution

Frequently Asked Questions

Floating Icon 1Floating Icon 2