我想在上面定义的each
函数的闭包中获取一个变量。但返回null:
public function saveSettingsImages(Collection $collection) : array
{
$arrImages = Array();
$collection->each(function (UploadedFile $file) use ($arrImages) {
$filename = $file->store('sliders', ['disk' => 'public']);
echo $filename; // it prints name of file as well
array_push($arrImages,$filename);
});
dd($arrImages);//it is empty array
return $arrImages;
}
但是dd($arrImages);
为空!
答案 0 :(得分:5)
尝试像这样将引用传递给变量
$collection->each(function (UploadedFile $file) use (&$arrImages) {
$filename = $file->store('sliders', ['disk' => 'public']);
echo $filename; // it prints name of file as well
array_push($arrImages,$filename);
});