When building a website or application with Laravel, you need a database to store your data. One of the best tools Laravel gives you for working with databases is migrations.
In this post, I’ll explain what migrations are, why they are useful, and show you step-by-step how to create, update, and refresh a table using migrations.
What is a Migration?
A migration in Laravel is like a version control system for your database.
- It’s a PHP file where you write instructions to create or modify database tables.
- Instead of writing raw SQL manually, you write migrations, and Laravel handles running them.
- It keeps your database changes organized and trackable.
What Problem Does Migration Solve?
Imagine you’re working on a project with a team. One developer adds a new table to the database. How will you get the same table in your local database?
You could ask them for SQL queries, but that’s messy.
With migrations, all database changes are stored in files, so everyone on the team can run the same migrations and have the exact same database structure.
In short:
✅ No need to write SQL directly
✅ Easy to share database changes with others
✅ Keeps your database history organized
Step 1: Create a Migration to Add a Table
Let’s create a products
table using a migration.
Run this command:
php artisan make:migration create_products_table
This creates a file inside database/migrations
with a name like:
2025_03_05_123456_create_products_table.php
Open the file, and you’ll see something like this:
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('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 8, 2);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('products');
}
};
What’s Happening?
up()
– This method creates the table when you run the migration.down()
– This method deletes the table if you roll back the migration.
Run the Migration
php artisan migrate
This will create the products
table in your MySQL database.
Step 2: Insert Data Manually Using Tinker
To quickly insert some products, you can use Laravel Tinker (a command line tool for interacting with your app).
Run:
php artisan tinker
Then, insert some data like this:
use App\Models\Product;
Product::create(['name' => 'Laptop', 'price' => 999.99]);
Product::create(['name' => 'Phone', 'price' => 599.99]);
exit;
This will add two products to your products
table.
Step 3: Change Column Name and Refresh Table
Oops! Maybe you realize you want to rename the name
column to product_name
.
Edit the Migration
In real projects, you would usually create a new migration to rename columns, but since we are learning, let’s directly edit the existing migration (if you haven’t deployed the project yet).
Change this:
$table->string('name');
To:
$table->string('product_name');
Refresh the Migration
You’ll need to rollback all migrations and run them again so the table gets recreated with the new column.
php artisan migrate:refresh
This command:
- Deletes all tables.
- Runs all migrations again.
Important!
This deletes all data in the tables — so only use this in development, not on live websites.
Summary
Here’s a quick recap of what you learned:
Step | Command/Concept | What it Does |
---|---|---|
1 | make:migration | Create a new migration file |
2 | Write schema in up() | Define columns and table structure |
3 | php artisan migrate | Run migrations to create tables |
4 | php artisan tinker | Insert data directly into tables |
5 | Edit migration & migrate:refresh | Change columns and refresh tables |
Final Tip
Migrations are one of the most powerful features of Laravel, especially when working with teams. Learning them well will make your database work much easier!