Avoid map() When Not Transforming: Use each()

Punyapal Shah
Use each() for iteration side effects like sending emails; use map() exclusively when returning a transformed collection.

Using map() purely for side effects builds an unused array in memory. Use each() to communicate intent when performing actions without modifying the collection elements.

// BAD: Builds a useless array of nulls in memory
$users->map(function ($user) {
    $user->notify(new MonthlyReport());
});

// GOOD: Expresses iteration intent clearly without allocating memory
$users->each(function ($user) {
    $user->notify(new MonthlyReport());
});
  • map() constructs and returns a new transformed collection instance
  • each() performs side-effect actions and returns the original collection
  • Keeps memory footprint lower and code intent explicit
Tags: #Laravel #Collections #Best Practices
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips