Ultimate Laravel Migrations Guide 2025


Laravel Migrations are a database version control system that allows developers to define, modify, and share database schema changes programmatically using PHP code instead of raw SQL. They function like Git for your database, enabling teams to track and manage database structure changes across different environments.



What Are Laravel Migrations?

Migrations are PHP files stored in the database/migrations directory that contain instructions for creating, modifying, or deleting database tables and columns. Each migration file includes a timestamp in its filename, ensuring Laravel executes them in the correct chronological order.



Core Structure

Every migration file contains two essential methods:

  • up() method: Defines changes to apply to the database (creating tables, adding columns, indexes)
  • down() method: Reverses the operations performed by the up() method, providing rollback functionality

Here’s a basic migration structure:

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::drop('flights');
    }
};
Enter fullscreen mode

Exit fullscreen mode



Key Benefits

Version Control: Track database schema changes over time, similar to how Git manages code versions.

Team Collaboration: Multiple developers can work on database changes without conflicts, sharing schema modifications through code.

Environment Consistency: Ensure identical database structures across development, staging, and production environments.

Reproducibility: Easily recreate database schemas in new environments or after system failures.

Rollback Capability: Revert problematic changes quickly using the down() method.



Essential Migration Commands

Creating Migrations:

bashphp artisan make:migration create_flights_table

Running Migrations:

bashphp artisan migrate

Rolling Back Migrations:

bashphp artisan migrate:rollback

Fresh Migration (drops all tables and re-runs migrations):

bashphp artisan migrate:fresh

Refresh Migrations (rollback and re-migrate):

bashphp artisan migrate:refresh

Check Migration Status:

bashphp artisan migrate:status



How Migrations Work

Laravel maintains a special migrations table in your database that tracks which migration files have already been executed. When you run php artisan migrate, Laravel:

  1. Checks the migrations table to identify unexecuted migrations
  2. Runs only new migration files that haven’t been previously executed
  3. Updates the migrations table to record successful executions
  4. Ensures no migration runs more than once



Advanced Features

Custom Database Connections: Specify different database connections using the $connection property

Conditional Migrations: Skip migrations based on conditions using the shouldRun() method

Isolated Execution: Run migrations on single servers to prevent conflicts using --isolated flag

Force Migrations: Bypass confirmation prompts in production using --force flag



Schema Builder Integration

Migrations utilize Laravel’s Schema Builder, providing a database-agnostic way to define table structures. The Blueprint class offers intuitive methods for creating columns, indexes, and foreign key constraints without writing raw SQL.

Laravel Migrations represent a sophisticated approach to database management, transforming schema changes from manual database operations into trackable, reversible code that integrates seamlessly with your application’s development workflow.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *