Return new collection without modifying original collection
PHP Snippet 1:
$original = collect([1, 2, 3, 4, 5]);
$modified = $original->map(function ($el) {
return $el * 2;
});
dd($modified);
PHP Snippet 2:
=> Illuminate\Support\Collection {#3726
all: [
2,
4,
6,
8,
10,
],
}
PHP Snippet 3:
dd($original);
PHP Snippet 4:
=> Illuminate\Support\Collection {#3743
all: [
1,
2,
3,
4,
5,
],
}