How to Create and Rollback Migration in Laravel 11 | websolutioncode.com
How to Create and Rollback Migration in Laravel 11 | websolutioncode.com

How to Create and Rollback Migration in Laravel 11

Migrations in Laravel are an essential aspect of managing your database schema. They allow you to easily modify and share the structure of your database across different development environments. In this guide, we will walk through the process of Create and Rollback Migration in Laravel 11

Understanding Migrations:

Before diving into creating migrations, it’s important to understand what they are and why they are used. Migrations in Laravel are like version control for your database schema. They enable you to define and modify the structure of your database using PHP code. This means you can easily recreate your database schema across different environments, making development and deployment more consistent and manageable.

Creating Migrations:

To create a new migration in Laravel 8, you can use the make:migration Artisan command. Open your terminal or command prompt and navigate to your Laravel project directory. Then run the following command:

php artisan make:migration create_users_table

This will create a new migration file in the database/migrations directory with a name like timestamp_create_users_table.php. Open this file in your text editor.

Inside the migration file, you’ll find two main methods: up() and down(). The up() method is used to define the changes you want to make to your database schema, such as creating tables or adding columns. The down() method is used to reverse those changes if you need to rollback the migration.

Let’s say we want to create a users table with id, name, and email columns. Here’s how you can define this in your migration file:

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

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Running Migrations:

Once you’ve defined your migration, you can run it using the migrate Artisan command. Simply run the following command in your terminal:

php artisan migrate

This will execute all pending migrations and apply the changes to your database schema. If everything goes smoothly, you should see output confirming that the migration was successful.

Rolling Back Migrations:

If you need to rollback a migration, perhaps because you made a mistake or need to revert to a previous state, you can use the rollback Artisan command. Run the following command in your terminal:

php artisan migrate:rollback

This will rollback the last batch of migrations that were run. If you need to rollback multiple batches, you can specify the number using the --step option:

php artisan migrate:rollback --step=3

This will rollback the last 3 batches of migrations.

Conclusion:

In this guide, we’ve covered the basics of creating, running, and rolling back migrations in Laravel 8. Migrations are a powerful tool that make it easy to manage your database schema and keep it in sync across different environments. By understanding how to create and manipulate migrations, you’ll have greater control over your database structure and improve your development workflow.

Leave a Reply