PHP Development | Tutorial 13 | Laravel Migrations & Database Management

by | Oct 9, 2025 | PHP Development, Web App Development | 0 comments

Laravel Migrations & Database Management are the backbone of most web applications, storing user data, transactions, and application settings. Traditionally, developers managed databases by manually writing SQL scripts—a process prone to errors and inconsistencies.

Laravel solves this with Migrations, which act like version control for your database schema. Along with migrations, Laravel provides seeders and factories to manage test and sample data. For interviews, expect questions about migrations, rollback, schema design, and seeding.

What are Migrations in Laravel?

Migrations are PHP files that define how to create, modify, or drop database tables. Instead of manually running SQL queries, you can run a migration command, and Laravel updates the database for you.

👉 Example of a migration command:

php artisan migrate

This applies all new migrations to the database.

Creating a Migration

php artisan make:migration create_users_table

This generates a migration file inside database/migrations/.

Example 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');
}
  • up() → Defines how to apply changes.
  • down() → Defines how to rollback changes.

Running & Rolling Back Migrations

Run all migrations:

php artisan migrate

Rollback last batch:

php artisan migrate:rollback

Reset everything:

php artisan migrate:reset

Refresh (rollback + migrate):

php artisan migrate:refresh

👉 Very useful for developers testing schema changes.

Modifying Tables with Migrations

Need to update a table? Create a new migration.

php artisan make:migration add_age_to_users_table --table=users

Inside migration:

public function up() {
    Schema::table('users', function (Blueprint $table) {
        $table->integer('age')->nullable();
    });
}

👉 Never edit old migrations in production—always create a new one.

Seeders & Factories

Seeders

Seeders populate tables with initial or test data.

php artisan make:seeder UserSeeder

Example:

public function run() {
    DB::table('users')->insert([
        'name' => 'Admin',
        'email' => '[email protected]',
        'password' => bcrypt('password'),
    ]);
}

Run seeder:

php artisan db:seed --class=UserSeeder

Factories

Factories generate fake data using Faker.

php artisan make:factory UserFactory

Example:

public function definition() {
    return [
        'name' => $this->faker->name,
        'email' => $this->faker->unique()->safeEmail,
        'password' => bcrypt('password'),
    ];
}

👉 Generate 50 fake users:

User::factory()->count(50)->create();

Advantages of Migrations in Laravel

  • Version Control for Databases → Track schema changes over time.
  • Team Collaboration → Same schema across all environments.
  • Portability → Works across MySQL, PostgreSQL, SQLite, SQL Server.
  • Automation → No manual SQL execution needed.
  • Seeding & Testing → Quick sample data generation.

Common Beginner Mistakes

  • Editing old migration files instead of creating new ones.
  • Forgetting to define rollback logic in down() method.
  • Running migrate:fresh in production (wipes entire DB).
  • Not using seeders/factories, leading to manual test data entry.

Sample Interview Questions & Answers

Q: What is a migration in Laravel?

A: Migrations are version-controlled PHP files that manage database schema changes (create, modify, delete tables).

Q: How do you rollback the last migration in Laravel?

A: Use php artisan migrate:rollback.

Q: Difference between migrate:refresh and migrate:fresh?

A: refresh rolls back and re-runs migrations, preserving seeders. fresh drops all tables and re-runs migrations from scratch.

Q: What are seeders and why are they useful?

A: Seeders insert sample or default data into the database—useful for testing or setting up defaults.

Q: How do you generate fake data in Laravel?

A: Using factories with Faker (e.g., User::factory()->count(10)->create()).

Q: Can migrations be database-independent?

A: Yes—Laravel’s Schema Builder works across MySQL, PostgreSQL, SQLite, and SQL Server.

Mini Project Idea

👉 Build a Task Manager App with Migrations & Seeders:

  • Migration for tasks table (title, description, status).
  • Seeder to generate 10 test tasks.
  • Factory to create 50 random tasks for testing.

Closing Note

Laravel Migrations & Database Management are developer-friendly, portable, and testable. They are critical for building professional, collaborative web applications.

Why Choose Laravel?
→ Discover why Laravel dominates modern PHP development

Laravel Routing & Middleware
→ Control application flow with routes and middleware layers

Laravel MVC Architecture
→ Build scalable apps using Models, Views, and Controllers

Laravel Artisan & Productivity Tools
→ Supercharge your workflow with Artisan commands and dev tools

Advantages of Eloquent ORM
→ Master database operations with Laravel’s powerful ORM

Laravel Collections Explained
→ Simplify data handling with powerful collection methods for filtering, mapping, and transforming arrays effortlessly

Blade Templating in Laravel
→ Create dynamic, reusable, and clean front-end layouts effortlessly using Laravel’s powerful Blade templating engine

0 Comments

Submit a Comment

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