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 Ajax pagination example with search

Laravel Ajax pagination with search

November 27, 2021December 4, 2021

In this tutorial we will learn pagination in Laravel using Ajax jQuery. Laravel provides its own library to build the pagination html, which we can easily use in our html page using $model->links() method and $model->paginate() method to make a long list into pagination. But sometimes our requirements are different…

Read More
Laravel How to Find the laravel version and location

How to find installed laravel version?

March 5, 2022March 5, 2022

To find the installed laravel version in command line we can use artisan command and to find the laravel version in application code we can use app method. In this article i will show you to get the version of laravel version in both ways using command line and in…

Read More
Php Google Login or Signup in laravel 8 9

How to Implement Google Login or Signup in laravel 8 / 9 ?

March 22, 2022March 22, 2022

Google login or signup in laravel are demanding from start as it gives user to access the website in single click. Google is widely used social media platform therefore most of the users are on it and we can take leverage of it by providing the feature of login and…

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

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version