# Getting Started with Laravel Migrations: A Beginner's Guide

If you're just starting with Laravel, you've likely heard the term *"migrations"* — and you might be wondering:

> **What are Laravel migrations and why should I care?**

This post will walk you through the basics, helping you confidently create and manage database schemas using Laravel’s migration system.

---

## 🚀 What Are Migrations?

Migrations are Laravel’s way of **version-controlling your database schema** using PHP code instead of raw SQL.

Think of them like Git for your database tables:

* You can create, modify, and remove tables through migration files.
    
* You can share those changes with your team.
    
* You can track and rollback changes easily.
    

They make your database predictable, portable, and team-friendly.

---

## How to Create a Migration

Run the following Artisan command in your Laravel project:

```bash
php artisan make:migration create_users_table
```

This will generate a file in the `database/migrations/` directory, named something like:

```plaintext
2025_07_19_000000_create_users_table.php
```

> The timestamp keeps migrations in order — Laravel runs them sequentially.

---

## Writing the Schema

The generated migration file will have two methods: `up()` and `down()`.

Here’s an example:

```php
public function up(): void {
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamps();
    });
}

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

* `up()`: defines what happens when you run the migration.
    
* `down()`: defines how to revert that change (rollback).
    

This design ensures changes can be reversed when needed.

---

## Running the Migration

To apply all pending migrations:

```bash
php artisan migrate
```

Laravel will execute the `up()` methods of all migration files that haven’t been run yet.

Need to rollback the last migration batch?

```bash
php artisan migrate:rollback
```

Want a fresh start?

```bash
php artisan migrate:fresh
```

👉 These commands are life-savers when experimenting or testing.

---

## 🧩 Making Schema Changes Later

Let’s say you want to add a new column to the `users` table:

```bash
php artisan make:migration add_profile_to_users_table
```

Inside the file:

```php
public function up(): void {
    Schema::table('users', function (Blueprint $table) {
        $table->text('profile')->nullable();
    });
}

public function down(): void {
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('profile');
    });
}
```

---

## ✅ Best Practices for Migrations

* Use clear, descriptive names (`create_users_table`, not `something_table`)
    
* Always define the `down()` method to support rollbacks
    
* Use migrations along with seeders for a complete local setup
    
* Use `--create=` and `--table=` options to scaffold with intent
    

---

## Why Migrations Matter

In real-world applications, databases evolve constantly.

Migrations make schema changes:

* **Predictable** — you're not modifying tables manually
    
* **Portable** — changes are shared through code
    
* **Safe** — rollbacks are built-in
    
* **Trackable** — full history lives in Git
    

Once you start using them, you won’t go back to manual DB changes.

---

## Final Thoughts

Laravel migrations are one of the most powerful tools in the framework — and they’re built to be beginner-friendly.

So go ahead:

* Create a new migration
    
* Write your schema
    
* Run `php artisan migrate`
    
* Start building!
    

Have any questions about migrations? Drop them in the comments.

---

### 🔗 Resources

* [Laravel Migrations Documentation](https://laravel.com/docs/migrations)
    
* [Seeding Your Database in Laravel](https://laravel.com/docs/seeding)
    

---

### 🧩 GitHub Example

Want a working demo? 👉 [Laravel Controllers Example on GitHub](https://github.com/rohitdhiman91/laravel-migrations-example)

---

### 🙌 Let’s Connect!

If you enjoyed this article, share your thoughts or ask questions below. Follow me on [LinkedIn](https://www.linkedin.com/in/rohitdhiman91/) / [Substack](https://rohitdhiman91.substack.com/) / [GitHub](https://github.com/rohitdhiman91/) for more beginner-friendly Laravel tutorials.
