不要忘记不修改原始的Laravel 5.7集合,可以使用集合的map()
方法来创建一个新的修改后的集合。下面是一个示例代码:
$collection = collect([1, 2, 3, 4, 5]);
$newCollection = $collection->map(function ($item) {
return $item * 2;
});
$newCollection->dump(); // 输出: Illuminate\Support\Collection {#xxx ▼
#items: array:5 [▼
0 => 2
1 => 4
2 => 6
3 => 8
4 => 10
]
}
$collection->dump(); // 输出: Illuminate\Support\Collection {#xxx ▼
#items: array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
}
在上面的示例中,我们使用map()
方法将原始的集合中的每个元素乘以2,并创建了一个新的集合$newCollection
。原始的集合$collection
保持不变。
上一篇:不要完成Gradle构建运行。
下一篇:不要为不同的ID选择多个单选按钮