【发布时间】:2022-01-21 12:15:06
【问题描述】:
我想使用 each() 循环从现有集合构建一个新集合:
$spot = Spot::where('slug', $slug)->first()->load('features.group');
$test = collect();
$spot->features->each(function ($item, $key) {
$current = collect([
$item->group->name => $item->name,
]);
$test->mergeRecursive($current);
});
dd($test);
我从循环内的代码中收到错误 Undefined variable $test。如何从循环内部访问$test 集合?
谢谢!
【问题讨论】: