Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to create migration in laravel

How to Create Migration in Laravel ?

Aman Jain, February 15, 2022February 22, 2022

Migrations are used to manage the version control among the developers so that they can sync database schema to each other. Laravel itself provides database migration capabilities to manage database schema updates like create table, alter, modify, drop etc.

Main advantage of migrations are to maintain the same schema across the team mates and you can update database schema without accessing the ssh or MySQL console.

In this article i will show you to create a simple table with auto increment column, title column and date columns.

For this purpose laravel provides an artisan command make:migration to generate the migrations.

Let’s understand the create migration in laravel step by step

Step 1 : Generate migration file

To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command

php artisan make:migration create_movies_table

Above command will create a migration file in folder database/migrations

Output: 
Created Migration: 2022_02_15_164343_create_movies_table

Step 2 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to add some more columns to schema of movies table.

so let’s open the file and start editing

<?php

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

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

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

In the file there is a class named as CreateMoviesTable and it extends Migration class. CreateMoviesTable contains two methods one is up and other one is down.

Up is used to update the database scheme and down method is used to rollback the changes of this migration. As you can see we are creating table movies in up method and dropping the table in down method.

let’s add some more fields to our table before updating the database.

 public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->id();
            $table->string("title");
            $table->text("description");
            $table->timestamps();
        });
    }

Step 3 : Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

This will create table in database and the output

Output: 
Migrating: 2022_02_15_164343_create_movies_table
Migrated:  2022_02_15_164343_create_movies_table (33.58ms)

Screenshot:

Screenshot 2022 02 15 at 10.29.48 PM
Laravel migration in database

How to rollback a migration in laravel ?

Once you created a migration and executed the php artisan migrate you want to revert this migration, so in this case we also write our logic in down method to rollback the our migration.

We can easily rollback our migration using below command

php artisan migrate:rollback

This will rollback the last migration. If you want to rollback last 5 or 6 migrations then you can use --step option

php artisan migrate:rollback --step=5

To rollback all migrations

php artisan migrate:reset

Also Read : How to run raw SQL query in migration Laravel ?

Related

Php Laravel laravelmigrationphp

Post navigation

Previous post
Next post

Related Posts

Php Laravel hasMany

How to Use hasMany Relationship in Laravel with Example ?

February 27, 2022November 10, 2023

Laravel hasMany relationship is used to create the relation between two tables. hasMany means create the relation one to Many. For example if a article have comments and we wanted to get all comments of the article then we can use hasMany relationship . Database Relations are used to create…

Read More
Php Laravel slug generator using trait

Laravel reusable slug generator using trait

November 8, 2021November 8, 2021

In our last article Laravel slug generator with example we demonstrate how we can generate slug for model but in that article we need to rewrite the same code for multiple models if we want use the slug generation in multiple model. so in this article i will show you…

Read More
Php Laravel 8 ajax form validation

Laravel 8 Ajax form validation with example

October 20, 2021November 16, 2021

In this article, I will demonstrate to show Laravel server side validation using Ajax. We will use jQuery to show the validation and submit the form. Using the jQuery and AJAX we will prevent the page reloading and show the validation. In this article, we will use the same simple…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version