How to Create Migrations and Seeders in Laravel for Beginners

In Laravel, you don't need to create database tables manually through phpMyAdmin. There is Migration to manage your table structure through code, and Seeder to populate initial dat...

How to Create Migrations and Seeders in Laravel for Beginners

In Laravel, you don't need to create database tables manually through phpMyAdmin. There is Migration to manage your table structure through code, and Seeder to populate initial data. Together they make your database easy to move and replicate. This article covers both from the ground up.

What Is a Migration?

A migration is like "version control" for your database. The table structure is defined in a file, so any team can build the same database with a single command.

Step 1: Creating a Migration

php artisan make:migration create_posts_table

A new file appears in database/migrations/. Define the columns in the up() method:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('content');
        $table->boolean('is_published')->default(false);
        $table->timestamps();
    });
}

Step 2: Running the Migration

php artisan migrate

Other useful commands:

php artisan migrate:rollback   # undo the last migration
php artisan migrate:fresh      # drop all tables & re-migrate
php artisan migrate:status     # view migration status

Step 3: Adding a Column to an Existing Table

To modify an existing table, create a new migration:

php artisan make:migration add_slug_to_posts_table --table=posts
public function up()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->string('slug')->after('title');
    });
}

What Is a Seeder?

A seeder populates a table with initial data (dummy data for testing or master data such as categories). Create a seeder:

php artisan make:seeder PostSeeder

Fill in the run() method:

public function run()
{
    Post::create([
        'title' => 'First Article',
        'content' => 'Article content...',
    ]);
}

Running the Seeder

Register the seeder in DatabaseSeeder.php, then run it:

$this->call(PostSeeder::class);   // inside DatabaseSeeder
php artisan db:seed
php artisan migrate:fresh --seed   # re-migrate + seed at once

Bonus: Bulk Dummy Data with a Factory

To create a lot of data at once, combine a seeder with a factory:

Post::factory()->count(50)->create();

Conclusion

Migrations and Seeders make managing a Laravel database tidy, portable, and easy to reproduce. Use migrations for table structure, seeders for initial data, and factories for bulk dummy data. If you run into a table error during migration, see our guide on fixing the SQLSTATE Base Table Not Found error.

migration laravel seeder laravel artisan migrate cara membuat tabel laravel database seeder laravel
Read Also

Here are some related articles that might help expand your knowledge:

Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →