Faker 和 Laravel 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29448404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Faker and Laravel 5
提问by user3732216
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.
我想弄清楚是否有人知道执行此任务的方法。我想尝试告诉用户插入的每种类型的角色有多少。这样我就可以说我只想要角色 4 中的 1 个和角色 3 中的 2 个,其余的都是一个。我敢肯定会有一些额外的逻辑,但不确定应该如何编写这样的东西。
<?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)
]);
}
}
}
回答by mirza
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.
您可以尝试对特定数量的 role_id 使用不同的 foreach,并根据需要修复每个 foreach 中的 id。
<?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)
]);
}
}
}