Reputation: 433
Simple question about factories:
ModelFactory:
$myarray=[
'cat1',
'cat2',
'cat3'
];
$factory->define(App\Job::class, function (Faker\Generator $faker) {
return [
'title' => $faker->randomElement($myarray),
'firm_id' => $faker->numberBetween($min = 1, $max = 3),
'user_id' => $faker->numberBetween($min = 1, $max = 5),
];
});
Why it's not working?
And another simple question: Is it possible to pass $myarray in factory from eloquent?
Upvotes: 0
Views: 298
Reputation: 2929
You need to make the closure use your array, to make it available in it :)
$myarray = ['cat1', 'cat2', 'cat3'];
$factory->define(App\Job::class, function (Faker\Generator $faker) use ($myarray) {
return [
'title' => $faker->randomElement($myarray),
'firm_id' => $faker->numberBetween($min = 1, $max = 3),
'user_id' => $faker->numberBetween($min = 1, $max = 5),
];
});
Here is a few examples from the php docs :) http://php.net/manual/en/functions.anonymous.php#example-160
Upvotes: 5