I'm trying to figure out if someone knows a way to perform this task. I am wanting to try and tell how many of each type of role to have for the users inserted. That way I can have it say I only want 1 other of role number 4 and 2 of role number 3 and have the rest be one. I'm sure there's going to be some additional logic but not sure how something like this should be written.
<?php
use Illuminate\Database\Seeder;
// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;
use App\User;
class UsersTableSeeder extends Seeder {
public function run()
{
// use the factory to create a Faker\Generator instance
$faker = Faker::create();
$roleIds = App\Role::lists('id');
User::create([
'first_name' => 'Me',
'last_name' => 'Me',
'username' => 'me',
'email' => '[email protected]',
'password' => 'secret',
'active' => 1,
'role_id' => 1
]);
foreach(range(2, 100) as $index) {
User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => str_replace('.', '_', $faker->unique()->userName),
'email' => $faker->email,
'password' => $faker->word,
'active' => $faker->boolean($chanceOfGettingTrue = 90),
'role_id' => $faker->randomElement($roleIds)
]);
}
}
}
You may try to use different foreach's for specific number of role_id's and fix the id in each foreach based on what you need.
<?php
use Illuminate\Database\Seeder;
// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;
use App\User;
class UsersTableSeeder extends Seeder {
public function run()
{
// use the factory to create a Faker\Generator instance
$faker = Faker::create();
$roleIds = App\Role::lists('id');
User::create([
'first_name' => 'Me',
'last_name' => 'Me',
'username' => 'me',
'email' => '[email protected]',
'password' => 'secret',
'active' => 1,
'role_id' => 1
]);
foreach(range(1, 2) as $index) {
User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => str_replace('.', '_', $faker->unique()->userName),
'email' => $faker->email,
'password' => $faker->word,
'active' => $faker->boolean($chanceOfGettingTrue = 90),
'role_id' => 1
]);
}
foreach(range(1, 3) as $index) {
User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => str_replace('.', '_', $faker->unique()->userName),
'email' => $faker->email,
'password' => $faker->word,
'active' => $faker->boolean($chanceOfGettingTrue = 90),
'role_id' => 2
]);
}
foreach(range(2, 100) as $index) {
User::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'username' => str_replace('.', '_', $faker->unique()->userName),
'email' => $faker->email,
'password' => $faker->word,
'active' => $faker->boolean($chanceOfGettingTrue = 90),
'role_id' => rand(3,5)
]);
}
}
}