I'm not sure how others feel about this, but when seeding the database, I like to use the actual models for the seeding, that way you can use all the handy functions that come with inserting related models.
If you're seeding the user and roles with predefined ids, then it's easy.
class UserSeeder extends Seeder
{
public function run()
{
DB::table('users')->truncate();
DB::table('roles')->truncate();
DB::table('user_roles')->truncate();
$users = [];
$user = User::create(['id' => 1, 'blah' => 'honk']);
$users[$user->id] = $user;
$user = User::create(['id' => 2, 'blah' => 'honk']);
$users[$user->id] = $user;
// etc etc
$roles = [];
$role = Role::create(['id' => 1, 'blah' => 'honk']);
$roles[$role->id] = $role;
// etc etc
$user[1]->roles()->sync(1);
$user[2]->roles()->sync(2);
$user[3]->roles()->sync(1, 3, 5);
}
}
In a recent application I wrote, I created a seeder that created the ACL groups, then permissions, assigned the permissions to the specific groups, then randomly created somewhere between 10-30 (using rand
) users and randomly assigned them to different groups.
Let me know if anything is unclear.