Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

You could, But that would be more of a hack (I.e by making some variable global) than an actually good solution. Just use the foreach constraint of php instead.

The each()-method is just a simple wrapper over a foreach, to add some "syntactic sugar" if you need to apply something to all elements in a collection.

Last updated 2 years ago.
0

You might want map

use Illuminate\Support\Collection;

$collection = new Collection([1,2,3]);

$doubledValues = $collection->map(function($value) {
  return $value * 2;
});

//$doubledValues->toArray() is now [2,4,6]

//$collection->toArray() is still [1,2,3]
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.