如何将变量传递给 Laravel 中的工厂?
How do you pass variables to a Factory in Laravel?
我如何 do/can 我有选择地将数字传递给 Laravel 工厂,这样如果我不提供数字,它会执行默认操作,如果我提供,它会使用它们?
例如我有一个 Student
模型。它生成一个随机的男性或女性学生,有 50% 的机会使用 Faker
的中间名,如预期的那样。它看起来像这样:
$factory->define(App\Student::class, function (Faker $faker) {
$gender = $faker->randomElement($array = array ('m','f'));
$has_middle_name = $faker->boolean($chanceOfGettingTrue = 50);
$student = [];
if ($gender == 'm') {
$student['first_name'] = $faker->firstNameMale;
if ($has_middle_name) {
$student['middle_name'] = $faker->middleNameMale;
}
} else {
$student['first_name'] = $faker->firstNameFemale;
if ($has_middle_name) {
$student['middle_name'] = $faker->firstNameMale;
}
}
$student['last_name'] = $faker->lastName;
$student['gender'] = $gender;
$student['birth_date'] = $faker->dateTimeBetween($startDate = '-18 years', $endDate = '-4 years', $timezone = null);
return $student;
});
我看到我可以使用 State 作为传递变量的方法。例如我可以这样做:
$student = factory(App\Student::class)->state('male')->make();
这将调用我工厂的这一点,因此修改 first_name
、middle_name
和 gender
属性以与 male
:
一致
$factory->state(App\Student::class, 'male', function (Faker $faker) {
$student = [];
$student['first_name'] = $faker->firstNameMale;
$has_middle_name = $faker->boolean($chanceOfGettingTrue = 50);
if ($has_middle_name) {
$student['middle_name'] = $faker->middleNameMale;
}
$student['gender'] = 'm';
return $student;
});
但是,我上面的代码创建了一个年龄在 4 到 18 岁之间的学生。如果我想创建一个特定年龄的学生怎么办?我知道我可以这样做来培养 8 到 9 岁的学生:
$birth_date = $faker->dateTimeBetween($startDate = '-9 years', $endDate = '-9 years', $timezone = null);
$student = faker(App\Student::class)->make(['birth_date' => $birth_date]);
但我宁愿这样做:
$student = faker(App\Student::class)->make($minAge = 8, $maxAge = 9);
这可能吗??
您可以将 min_age
和 max_age
定义到您的 Student 模型工厂并使用以下代码:
$student = faker(App\Student::class)->make(['min_age' => 8, 'max_age' => 9]);
我如何 do/can 我有选择地将数字传递给 Laravel 工厂,这样如果我不提供数字,它会执行默认操作,如果我提供,它会使用它们?
例如我有一个 Student
模型。它生成一个随机的男性或女性学生,有 50% 的机会使用 Faker
的中间名,如预期的那样。它看起来像这样:
$factory->define(App\Student::class, function (Faker $faker) {
$gender = $faker->randomElement($array = array ('m','f'));
$has_middle_name = $faker->boolean($chanceOfGettingTrue = 50);
$student = [];
if ($gender == 'm') {
$student['first_name'] = $faker->firstNameMale;
if ($has_middle_name) {
$student['middle_name'] = $faker->middleNameMale;
}
} else {
$student['first_name'] = $faker->firstNameFemale;
if ($has_middle_name) {
$student['middle_name'] = $faker->firstNameMale;
}
}
$student['last_name'] = $faker->lastName;
$student['gender'] = $gender;
$student['birth_date'] = $faker->dateTimeBetween($startDate = '-18 years', $endDate = '-4 years', $timezone = null);
return $student;
});
我看到我可以使用 State 作为传递变量的方法。例如我可以这样做:
$student = factory(App\Student::class)->state('male')->make();
这将调用我工厂的这一点,因此修改 first_name
、middle_name
和 gender
属性以与 male
:
$factory->state(App\Student::class, 'male', function (Faker $faker) {
$student = [];
$student['first_name'] = $faker->firstNameMale;
$has_middle_name = $faker->boolean($chanceOfGettingTrue = 50);
if ($has_middle_name) {
$student['middle_name'] = $faker->middleNameMale;
}
$student['gender'] = 'm';
return $student;
});
但是,我上面的代码创建了一个年龄在 4 到 18 岁之间的学生。如果我想创建一个特定年龄的学生怎么办?我知道我可以这样做来培养 8 到 9 岁的学生:
$birth_date = $faker->dateTimeBetween($startDate = '-9 years', $endDate = '-9 years', $timezone = null);
$student = faker(App\Student::class)->make(['birth_date' => $birth_date]);
但我宁愿这样做:
$student = faker(App\Student::class)->make($minAge = 8, $maxAge = 9);
这可能吗??
您可以将 min_age
和 max_age
定义到您的 Student 模型工厂并使用以下代码:
$student = faker(App\Student::class)->make(['min_age' => 8, 'max_age' => 9]);