You need not worry about being new to Laravel and wondering what laravel database migrations are. Here we are going to explain it like you’re a kid building LEGO blocks. This discussion will teach you how to use Laravel migrations step by step — using Laravel 12.x and easy-to-understand code examples.
Let’s classify it!
What Is Laravel Database Migration?
Building a house (your app) requires you to manage all the available rooms (tables in a database). Laravel migrations are like blueprints that tell Laravel how to create your house (your database).
It helps to:
- Create tables
- Add or change columns
- Share your database setup with your team
- Revert changes easily
All this — using PHP code instead of SQL! It’s amazing, right?
Step 1: Create a Laravel Project
Ensure that you have installed Laravel 12.x before proceeding. if want to know how to install Laravel latest version in your pc or mac or linux. check out our post install laravel on your pc.
To create a new Laravel project:
bash composer create-project laravel/laravel laravel-migration-demo cd laravel-migration-demo php artisan serve
Step 2: Create a Laravel Database Migration
For blog posts, we need to make a table.
Run this command:
bash php artisan make:migration create_posts_table
This creates a new file in database/migrations/ — something like this. here is a Laravel migration example.
php database/migrations/2025_04_15_065332_create_posts_table.php
You can find it in our project folder. Open your project folder go to database folder then, migrations folder, here you can find the file.
Open that file, and you’ll see something like
php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('posts'); } };
What this means:
- Schema::create(‘posts’) → Make a table called posts
- $table->id() → Add an ID column (primary key)
- $table->string(‘title’) → Add a “title” column (up to 255 characters)
- $table->text(‘content’) → Add a long text “content” column
- $table->timestamps() → Adds created_at and updated_at columns automatically
Read More Blog Posts
How to use Laravel Queues Beginner’s Guide
Tips and Tricks for Running Commands with Laravel Process
Laravel Notifications Made Easy: A Step-by-Step Guide
Step 3: Run the Migration
In the database, if we actually want to create the table, we should run:
bash php artisan migrate
Done! Your posts table is now created inside the database.
Step 4: Rolling Back (Undoing a Migration)
If you made a mistake,you can roll back the last migration using:
bash php artisan migrate:rollback
This removes the table (by running the down() method). Like a magical “undo” button!
Step 5: Changing a Table (Add/Remove Columns)
Now you want to include an “author” column to the posts table.
Run:
bash php artisan make:migration add_author_to_posts_table
In the new file:
php public function up(): void { Schema::table('posts', function (Blueprint $table) { $table->string('author')->after('title'); }); } public function down(): void { Schema::table('posts', function (Blueprint $table) { $table->dropColumn('author'); }); }
Then run:
bash php artisan migrate
Congrats! — Your posts table now has an author column.
Step 6: Reset or Refresh the Database (Useful in Development)
To wipe and re-run all migrations:
bash php artisan migrate:refresh
To remove everything along the data and seed fake records again:
bash php artisan migrate:refresh --seed
This is great when testing and building.
Extra Tip: Use Laravel 12 Seeder with Migration
After creating tables, do you want to fill your database with fake data?
Edit your seeder file database/seeders/DatabaseSeeder.php:
php use App\Models\Post; public function run(): void { Post::factory()->count(10)->create(); }
Make sure you have a factory. Don’t get overwhelmed; we will cover database seeding in our future post. Here we just give you some insight inside what you can do with laravel database seeding.
bash php artisan make:factory PostFactory --model=Post
In the factory file, include:
php public function definition(): array { return [ 'title' => fake()->sentence(), 'content' => fake()->paragraph(), 'author' => fake()->name(), ]; }
Run the seeder:
bash php artisan db:seed
A filled database including 10 posts is now available!
Laravel Migration Commands You Should Know
Command | What It Does |
php artisan migrate | Run all new migrations |
php artisan migrate:rollback | Undo the last migration |
php artisan migrate:refresh | Reset everything and re-run migrations |
php artisan migrate:fresh | Delete all tables and run fresh migrations |
php artisan make:migration | Create a new migration file |
Quick Recap
Laravel migrations are like blueprints for your database. You can create tables and add or change columns using simple code. Migrations help teams work together without breaking stuff. Laravel has awesome rollback and refresh tools built-in.
Conclusion
Now you’ve learned one of the most important skills in Laravel development — database migrations. Now you are ready to build organized, professional, and easy updates.
I think you have enjoyed this tutorial, so if you have queries or want a full CRUD tutorial next? Just drop a comment!