Php 如何在相关工厂laravel中传递变量
我正在Lumen上创建PHP单元测试。我尝试将变量$name传递给相关的factory Template-->属性 我已经尝试过这个代码,但不起作用Php 如何在相关工厂laravel中传递变量,php,laravel,factory,Php,Laravel,Factory,我正在Lumen上创建PHP单元测试。我尝试将变量$name传递给相关的factory Template-->属性 我已经尝试过这个代码,但不起作用 $name='Mulyawan Sentosa'; 工厂(TemplateModel::class)->创建( [ “id”=>12 ] )->各( 函数($template){ $template->attributes()->保存( 工厂(TemplateattributeModel::class)->make( [ “name”=>$name
$name='Mulyawan Sentosa';
工厂(TemplateModel::class)->创建(
[
“id”=>12
]
)->各(
函数($template){
$template->attributes()->保存(
工厂(TemplateattributeModel::class)->make(
[
“name”=>$name
]
)
);
}
);
如何做到这一点?谢谢大家! 您需要在使用关键字参数中传递变量,才能在闭包函数中使用变量
$name = 'Mulyawan Sentosa';
factory(TemplateModel::class)->create(
[
'id' => 12
]
)->each(
function ($template) use($name) {
$template->attributes()->save(
factory(TemplateattributeModel::class)->make(
[
'name' => $name
]
)
);
}
);
欲了解更多信息