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.
May 14, 2015
8
Level 6
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?
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
Please or to participate in this conversation.