Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>foreach</code> statement should be used as a sort of a way to cycle through a collection and perform some sort of logic on it. If what is in it effects other things in the program, then use this loop. </p> <p>The <code>.each</code> method uses <code>array_map</code> to cycle through each of the objects in the collection and perform a closure on each one. It then returns the resulting array. That is the key! <code>.each</code> should be used if you want to change the collection in some way. Maybe it's an array of cars and you want to make the model upper case or lower case. You would just pass a closure to the <code>.each</code> method that takes the object and calls <code>strtoupper()</code> on the model of each Car object. It then returns the collection with the changes that have been made. </p> <p>Morale of the story is this: use the <code>.each</code> method to change each item in the array in some way; use the <code>foreach</code> loop to use each object to affect some other part of the program (using some logic statement).</p> <h3>UPDATE (June 7, 2015)</h3> <p>As stated so Eloquently (see what I did there?) below, the above answer is slightly off. The <code>.each</code> method using <code>array_map</code> never actually used the output from the <code>array_map</code> call. So, the new array created by <code>array_map</code> would not be saved on the <code>Collection</code>. To change it, you're better off using the <code>.map</code> method, which also exists on a <code>Collection</code> object.</p> <p>Using a <code>foreach</code> statement to iterate over each of them makes a bit more sense because you won't be able to access variables outside the Closure unless you make sure to use a <code>use</code> statement, which seems awkward to me.</p> <p>The implementation when the above answer was originally written can be <a href="https://github.com/laravel/framework/blob/4.0/src/Illuminate/Support/Collection.php#L171-L182" rel="noreferrer">found here</a>.</p> <h3><code>.each</code> in Laravel 5.1</h3> <p>The new <code>.each</code> that they are talking about below no longer uses <code>array_map</code>. It simply iterates through each item in the collection and calls the passed in <code>$callback</code>, passing it the item and its key in the array. Functionally, it seems to work the same. I believe using a <code>foreach</code> loop would make more sense when reading the code. However, I see the benefits of using <code>.each</code> because it allows you to chain methods together if that tickles your fancy. It also allows you to return false from the callback to leave the loop early if your business logic demands you to be able to.</p> <p>For more info on the new implementation, check out <a href="https://github.com/laravel/framework/blob/5.1/src/Illuminate/Support/Collection.php#L99-L114" rel="noreferrer">the source code</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload