简体   繁体   English

Laravel 5.3模型工厂

[英]Laravel 5.3 Model Factories

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? 还有一个简单的问题:能否雄辩地在工厂中传递$ myarray?

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 这是来自php docs的一些示例:) http://php.net/manual/zh/functions.anonymous.php#example-160

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM