我有用户迁移:
$table->enum('type',['seller','buyer'])->default('seller');
我想在使用ModelFactory时如何获得随机价值卖家或买家?
$factory->define(App\User::class, function (Faker\Generator $faker) {
static $password;
return [
'firstName' => $faker->name,
'lastName' => $faker->name,
'username' => $faker->unique()->username,
'email' => $faker->unique()->safeEmail,
'password' => md5('user123'),
'bio' => $faker->sentence(3, true),
'type' => ???,
];
});
答案 0 :(得分:25)
使用randomElement
方法
'type' => $faker->randomElement(['seller', 'buyer']),
答案 1 :(得分:0)
Laravel版本> = 5.6
use Illuminate\Support\Arr;
$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);
// 4 - (retrieved randomly)
"type" => Arr::random($array);