Laravel migration

Migration is version control of the database, he geiv us a programing way to define the database structure of a table.

so if you make a mistake at any time all you need is to roll back the migration.

The use of migration to create edit a database is more efficient than creating a database table manual.

How to create a migration

to create a migration in laravel we prefer to use the artisan command make:migration

Step1: create a migration

php artisan make:migration create_news_table

laravel create migration using artisan command

all your migration fill will be generated in app/database/migrations directory.

laravel migration directory

once you open the migration file you will see two function

  1. up() - used when you run the migration
  2. down() - when you rollback the migration or undo anything on up function

Step2: write your schema

now the new migration will look like


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

class CreateUsersTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('users', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

UP function in the migration file

The first line used to create a users table  Schema::create('users', function (Blueprint $table) 
  1. $table->bigIncrements('id'); // create id column with data type bigInt
  2. $table->string('name'); // create name column with data type text
  3. $table->string('email')->unique();// create email column with data type text and index unique
  4. $table->timestamp('email_verified_at')->nullable(); // create email_verified_at column with data type timestamp and make it nullable 
  5. $table->string('password');// create name column with data type text
  6. $table->rememberToken();
  7. $table->timestamps();// create created_at,updated_at column with data type timestamp  

Down function in the migration file

 The first line Schema::dropIfExists('users');  will rollback and undo the up function

How to roll back migration

to roll back the latest migration , you may use the rollback command.

When you roll back the migration you will lose all your data so be careful when you are in production mode

php artisan migrate:rollback

 Step3: run the migration

you can use the PHP artisan command to run migration

php artisan migrate