how to create migration from existing database in laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Create Laravel Migrations from an Existing Database: A Developer's Guide
As a senior developer, I often encounter situations where we have an existing production database, and we need to synchronize our application’s structure—defined by Laravel migrations—with that live data. The confusion arises because Laravel migrations are fundamentally blueprints for *defining* the schema, not direct tools for dumping or importing raw SQL structures from an arbitrary source.
You rightly point out that you know how to generate a migration file, but now you want to reverse the process: populate your Laravel structure based on what already exists in MySQL. Understanding this process requires distinguishing between the **schema** (the table definitions) and the **data** (the rows inside the tables).
This guide will walk you through the practical methods developers use to bridge the gap between an existing database and the Laravel migration system.
## The Fundamental Distinction: Schema vs. Data
Before diving into commands, it is crucial to understand the roles of migrations versus data.
1. **Migrations (Schema Definition):** These files dictate *how* your tables should look (create tables, add columns, define indexes). They are essential for version control and deployment.
2. **Data (Content):** This is the actual information stored within those tables.
If your existing MySQL database already has the correct table structures, you don't necessarily need to recreate the migration file from scratch. Instead, you need to ensure that your Laravel application’s migration system recognizes and manages these existing structures correctly.
## Method 1: Schema Synchronization via Inspection (The Manual/Advanced Approach)
If you have a complex, pre-existing database that you want to use as the starting point for future development, direct automated conversion is often risky. A safer approach involves inspecting your live database structure and manually creating or modifying migrations to match it.
### Step 1: Inspecting the Existing Schema
Use your MySQL client (like phpMyAdmin, MySQL Workbench, or the command line) to view the `CREATE TABLE` statements for all relevant tables in your existing database. Note down every column name, data type, and constraint.
### Step 2: Generating/Modifying Migrations
Based on the inspection, you manually create a new migration file (e.g., `php artisan make:migration create_users_table --create=users`) or modify an existing one to precisely reflect the structure of your live tables.
**Example of a Manual Migration:**
```php
// database/migrations/2023_10_27_create_products_table.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name'); // Matches existing column type
$table->decimal('price', 8, 2); // Matches existing data type
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
```
This method gives you complete control, ensuring that your Laravel history is accurate while reflecting the state of your actual data source. Remember, understanding how Eloquent interacts with these structure definitions is key to effective database management on **https://laravelcompany.com**.
## Method 2: Populating Data via Seeders (The Practical Approach)
Once you have established the correct migration structure that mirrors your existing tables, the next step is populating those structures with data. This is where Laravel's Seeding mechanism shines. You don't need to recreate the database; you just need to fill it with meaningful content using Factories and Seeders.
### Creating a Seeder
Create a seeder file to populate your newly defined tables:
```bash
php artisan make:seeder ProductSeeder
```
Inside `ProductSeeder.php`, you would use Eloquent models to insert records, perhaps pulling existing data or generating new sample data based on the structure defined in your migrations.
```php
// database/seeders/ProductSeeder.php
use App\Models\Product;
use Illuminate\Database\Seeder;
class ProductSeeder extends Seeder
{
public function run(): void
{
// Insert data that matches the schema defined in your migrations
Product::create([
'name' => 'Existing Product A',
'price' => 49.99,
]);
// You can use this to seed initial configurations or test entries
// This focuses on populating the data layer, not redefining the schema layer.
}
}
```
Finally, run the