Laravel

Laravel Migrations Explained: Creating and Modifying Tables

Administrator
By Administrator
Published Oct 07, 2025
4 min read
Laravel Migrations Explained: Creating and Modifying Tables

Laravel Migrations Explained: Creating and Modifying Tables

Laravel migrations are like version control for your database schema. They allow you to modify your database structure programmatically and share those changes with your team. Migrations are one of Laravel's most powerful features for managing database changes across development, testing, and production environments. This comprehensive guide will walk you through everything you need to know about Laravel migrations.

What are Migrations?

Migrations are PHP classes that contain methods to modify your database schema. They allow you to:

  • Create tables, columns, and indexes
  • Modify existing tables and columns
  • Drop tables and columns safely
  • Version control your database changes
  • Collaborate with teams effectively

Creating Migrations

Using Artisan Commands

Generate migration files using Artisan commands:

// Create a basic migration
php artisan make:migration create_users_table

// Create a migration with a model
php artisan make:migration create_users_table --create=users

// Create a migration for modifying an existing table
php artisan make:migration add_last_login_to_users_table

// Create a migration with a specific table
php artisan make:migration add_votes_to_users_table --table=users

// Create a migration with path
php artisan make:migration create_posts_table --path=database/migrations/custom

// Create multiple migrations at once
php artisan make:migration create_users_table
php artisan make:migration create_posts_table
php artisan make:migration create_comments_table

Migration File Structure

Each migration file contains two methods:

  • up() - Creates or modifies tables
  • down() - Reverts the changes made in up()

The migration files follow a standard structure with proper imports and class definitions.

Working with Schema Builder

Working with Schema Builder

The Schema builder provides a clean, fluent interface for creating and modifying database tables. You can define columns, indexes, foreign keys, and constraints.

Common Column Types

Laravel supports all common database column types:

  • String: string(), text(), mediumText(), longText()
  • Integer: integer(), tinyInteger(), smallInteger(), mediumInteger(), bigInteger()
  • Numeric: decimal(), double(), float()
  • Boolean: boolean()
  • Date/Time: date(), time(), timestamp(), datetime()
  • JSON: json(), jsonb()
  • Special: uuid(), enum(), set()

Each column type supports modifiers for additional constraints.

Column Modifiers

Add constraints and additional properties to your columns:

  • Unique: ->unique()
  • Default: ->default('value')
  • Nullable: ->nullable()
  • Indexes: ->index(), ->primary(), ->unique()
  • Foreign keys: ->foreign()

Modifying Tables

Modifying Tables

You can modify existing tables using Schema::table(). Common operations include:

  • Adding new columns
  • Modifying existing columns
  • Dropping columns
  • Adding indexes
  • Adding foreign keys

Use Schema::dropIfExists() to safely drop tables when needed.

Running Migrations

Basic Migration Commands

Execute migrations with Artisan commands:

// Run all migrations
php artisan migrate

// Run migrations with force
php artisan migrate --force

// Rollback last migration
php artisan migrate:rollback

// Rollback last 5 migrations
php artisan migrate:rollback --step=5

// Rollback all migrations
php artisan migrate:rollback --step=9999

// Reset and run migrations (drop all tables)
php artisan migrate:fresh

// Reset and run migrations with force
php artisan migrate:fresh --force

// Check migration status
php artisan migrate:status

// Show pending migrations
php artisan migrate:status --pending

// Run specific migration
php artisan migrate --path=database/migrations/2023_01_01_000000_create_users_table.php

// Run migrations for specific batch
php artisan migrate --batch=1

Database Specific Migrations

Database-Specific Features

Each database system supports specific features:

  • MySQL: Engine options, character sets, spatial types
  • PostgreSQL: JSONB, array types, hstore, geospatial types
  • SQLite: Limited features, file-based storage

Check your database documentation for specific capabilities.

Migration Best Practices

Organizing Migration Files

Keep your migrations organized and maintainable:

// Group related migrations by feature
database/migrations/
├── 2023_01_01_000000_create_users_table.php
├── 2023_01_02_000000_create_profiles_table.php
├── 2023_01_03_000000_add_phone_to_users_table.php
├── 2023_02_01_000000_create_posts_table.php
├── 2023_02_02_000000_create_comments_table.php
└── 2023_02_03_000000_add_status_to_posts_table.php

// Use descriptive names
create_users_table
create_profiles_table
add_phone_to_users_table
add_email_verification_to_users_table
create_posts_table
add_comments_to_posts_table
create_categories_table
add_indexes_for_performance

Writing Safe Migrations

Write migrations that won't break your application:

// Always test migrations in development
php artisan migrate

// Check migration status before running
php artisan migrate:status

// Backup database before running migrations
mysqldump -u user -p database_name > backup.sql

// Use rollback option if needed
php artisan migrate:rollback --step=1

// Always provide rollback methods
public function down()
{
    // Safe rollback operations
}

// Don't drop columns in use - always check if column exists first

Always check if columns exist before dropping them to avoid breaking your application.

Real-World Migration Examples

Real-World Examples

Here are examples of migration files for common application patterns:

User System Migration

A complete migration for a user system with authentication fields and indexes.

Blog System Migration

A complete migration for a blog system with posts, categories, and relationships.

Each migration follows the standard structure with up() and down() methods.

Conclusion

Laravel migrations are an essential tool for managing database changes. By following best practices and understanding the full capabilities of the Schema builder, you can maintain a clean, organized database structure that evolves with your application.

Key takeaways:

  • Use descriptive migration file names
  • Always provide proper rollback methods
  • Test migrations in development before production
  • Use appropriate column types and modifiers
  • Create proper indexes for performance
  • Back up your database before major migrations
  • Keep migrations organized and maintainable

With migrations, you can confidently manage database changes across all your environments while maintaining a clean, structured database. Happy coding! 🚀

Related Articles

Using Laravel Sanctum for API Authentication

Using Laravel Sanctum for API Authentication

Oct 07, 2025

Introduction to Laravel Sanctum Laravel Sanctum is a simple authentication library for SPAs (Sing...

How to Send Emails in Laravel Using Mailables

How to Send Emails in Laravel Using Mailables

Oct 07, 2025

Introduction to Laravel Mailables Sending emails in Laravel has never been easier thanks to Maila...

Laravel Queues and Jobs: Complete Beginner's Guide

Laravel Queues and Jobs: Complete Beginner's Guide

Oct 07, 2025

What Are Laravel Queues? Laravel Queues allow you to defer processing of time-consuming tasks. In...

How to Handle File Uploads in Laravel

How to Handle File Uploads in Laravel

Oct 07, 2025

Introduction to File Uploads in Laravel File uploads are a fundamental feature in most web applic...