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

How to Rollback Migration Laravel ?

Aman Jain, March 6, 2022February 8, 2024

In our recent article I showed you about creating migration in laravel and sometimes after creating migration we want to rollback migration laravel so in this article i will show you to rollback migration in laravel. Laravel artisan have multiple options to rollback the migration like we can rollback all migrations, we can rollback last n number of migration using step option and in the same way we can rollback all specific rollback using reset option and refresh options.

In relational databases rollback is a technique to revert back the database operation in previous state. In the same way laravel maintains it migrations in file and database, and we can rollback the operation to its previous state using artisan command.

Simple syntax to Rollback Migration Laravel

Below command will rollback the last executed migrations

php artisan migrate:rollback


Rollback last migrations using step option

We can also rollback last migration using the step option in artisan command, step option accept numeric parameter to rollback last migrations. For example if --step =2 then it means it will rollback last two migrations.

here is the command

php artisan migration:rollback --step=3

Rollback last batch migrations

We can also rollback last batch of migration using the migration rollback command without any options as below

Here is the command

php artisan migration:rollback

Rollback all migrations and reset database

We can also rollback all migration and also can reset the database migration to start point using artisan migration:reset command

Here is the command

php artisan migration:reset

Rollback all and Re run all migrations

refresh option is used to rollback all migration and it runs again migration command to re create the database

Here is the command

php artisan migration:refresh

Let’s understand with an example

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 add_status_movies_table

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

Output: 
Created Migration: 2022_02_15_174050_add_status_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 AddStatusMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        
    }

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

In the file there is a class named as AddStatusMoviesTable and it extends Migration class. AddStatusMoviesTable 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::table('movies', function (Blueprint $table) {
             
            $table->integer("status");
            
        });
}

public function down()
{
        Schema::table('movies', function (Blueprint $table) {
             
            $table->dropColumn("status");
            
        });
}

Here we used Schema::table to update the table.

Most important thing while rollback is to add the logic of down for rollback here

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_174050_add_status_movies_table
Migrated:  2022_02_15_174050_add_status_movies_table (20.08ms)

Step 3 : Run Migration

Now if we want rollback the migration we need to rollback it using artisan command

php artisan migrate:rollback
or
php artisan migrate:rollback --step=1

Also Read : How to rollback a specific migration in laravel ?

Related

Laravel Laravel 10 Laravel 9 laravelmigrationrollback

Post navigation

Previous post
Next post

Related Posts

Php Laravel CRUD with Search, Image and Pagination

Laravel 10 CRUD Example Tutorial with Search, Image and Pagination

March 12, 2023July 3, 2024

This article will cover the implementation of CRUD operations along with Search, Image uploading, and Pagination in Laravel. In addition to CRUD operations, we will also cover form validation, unique validation, Flash messages, and viewing uploaded images. It is crucial to learn all aspects of CRUD and beyond, including uploading…

Read More
Php How to Create a Custom Artisan Command in LaravelHow to Create a Custom Artisan Command in Laravel

How to Create a Custom Artisan Command in Laravel ?

May 19, 2022May 26, 2022

Laravel artisan provides several inbuilt commands to help the developer to build a robust applications like to create the controller, models, migration etc. and in this article i will show you to create your own custom artisan command in laravel. This custom artisan can be configure according to your needs…

Read More
Php Laravel ajax login and register

Laravel ajax login and registration with example

December 4, 2021November 12, 2023

In Laravel 8 there is multiple ways to implement the laravel Ajax login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can…

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

  • 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

  • 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
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version