Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ctrlaltdelme's avatar

Populating DB Tables with static data?

Is there a recommendation or Laravel Way of populating the database with static data that your site needs to use? It doesn't come from a Model, but it's just static data the site needs. Things like maybe Categories, or available Tags (to pick when making a Post), or other data.

I think Seeders is the way, but I thought that was just for development, and not production

0 likes
8 replies
ctrlaltdelme's avatar

@Glukinho oh perfect. Seems the general consensus is migrations for production data and seeders for test data

Glukinho's avatar

@ctrlaltdelme some are against putting data in migrations, saying migrations are for database structure only. I personally share this opinion.

One reason can be: in migrations you operate on database level (tables and columns), while app data belongs to application level (models and fields along with app logic). It's wrong to mix them in one place.

1 like
ctrlaltdelme's avatar

So you'd say static data for the database should go in seeders? Even for production?

martinbean's avatar

@ctrlaltdelme Do it as part of the migration:

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

    DB::table('categories')->insert([
        ['name' => 'Category 1'],
        ['name' => 'Category 2'],
        // And so on...
    ]);
}

You can’t stop a migration run at an arbitrary point to go off and run some seeders, and then resume migrating, so it’s not going to work in automated environments such as CI and CD. Especially if any subsequent migrations rely on those records existing.

kevinbui's avatar

I have to disagree with this approach.

First, design-wise, this is not Single Responsibility. Migrations should only involve altering the database structure. Data population is the responsibilities of seeders or some place else.

Second, if this approach regularly used over time, test cases and CI/CD will slow down drastically. Because obviously migrations is run for each test case. We did this before and felt the pain. We can always populate data in test cases using factories or the seed method.

My recommendations are still below.

kevinbui's avatar

Although it is not highly recommended, you can still use seeders in whichever environment you like.

For a commercial project, I suggest using the laravel-deploy-operations package, which can populate data as part of deployment. It also keeps track of all the changes that have been made, just like migrations.

Please or to participate in this conversation.