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

apocalyarts's avatar

Insert data during migration

Hey guys! For my next application I need some rights management by putting users into groups. For convenience I want some default groups to be always present. In order to achieve this, I am performing a DB::insert() during my Migration, like this:

class CreateGroupsTable extends Migration {

    public function up()
    {
        Schema::create('groups', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name')->unique();
            $table->boolean('public');
        });

        DB::table('groups')->insert([
            ['name' => 'Developer', 'public' => true],
            ['name' => 'Journalist', 'public' => true],
            ['name' => 'Admin', 'public' => false],
        ]);
    }

    public function down()
    {
        Schema::drop('groups');
    }

}

Is this considered bad practise? Or should I use Database Seeds? Pro's and Con's?

0 likes
8 replies
JeroenVanOort's avatar

Other than using something to do something it isn't meant to do, which I consider to be bad practice, it doesn't really hurt in this case. In my own and my employers projects, we do use the seeders for this kind of things. It makes it easier to find what happens where in case of debug, which is especially in important when supervising interns as we do.

2 likes
janareit's avatar

I have kept migrations and seeds separately. Looks alot cleaner. Especially if you plan to use some factory for generating seeds later on.

And you can migrate and seed with one go anyhow.

php artisan migrate:refresh && php artisan db:seed

will do a full refresh to migrations and seeds.

mstnorris's avatar
Level 55

Use a ConstantsTableSeeder. That's how I handle user accounts and other items that I always want in my database. Things that won't change.

4 likes
apocalyarts's avatar

Good points. I was unsure because like Jeroen mentioned it doesn't really hurt in this case.

Going with mstnorris approach now, because this looks appropriate, considering that I may have additional default data in the future

leonardodecuritiba's avatar

I know that this is a very old post, but I want to share my point. Beyond the discussion of whether it's a good or bad practice, let's think together. In the migration process, perhaps you want to reverse the changes. So, by writing the insertion commands inside the migration, you can write some logic to reverse the changes. What you can't do in Seed in a practical way.

1 like

Please or to participate in this conversation.