7 Rich Text Editors for Modern Web Apps
A rich text editor lets people create and format content inside a web app, from a short comment to a structured document.…
In Laravel, a collection wraps an array and provides methods you can chain to filter, sort, and work with its values. tap(), pipe(), and each() pass different values along the chain. tap() gives its callback the whole collection and returns that collection. With pipe(), the callback receives the collection and its result becomes the return value. each() calls its callback once for every item and returns the collection. Laravel’s collection documentation describes these behaviors for Laravel 13.x.
Choose a method based on the callback’s job: use tap() to inspect the collection, pipe() to produce a result from it, or each() to act on its items. The next chained call receives the value that method returns.
tap() to inspect a collection and continuetap() passes the entire collection to its callback once, then returns that collection so the chain can continue. Use it to log or inspect an intermediate result while keeping the collection as the input to the next method.
$sorted = collect([2, 4, 3, 1, 5])
->sort()
->tap(function (Collection $items) {
Log::debug('Values after sorting', $items->values()->all());
})
->values();
tap() runs after sort(), and its callback logs the sorted values before values() receives the collection. sort() preserves the collection’s keys, while values() resets them.
tap() returns the collection so the chain can continue, but its callback can still change data or cause other side effects. If you only want to inspect the intermediate result, do not modify the collection inside the callback.
pipe() when the chain should return something elsepipe() passes the whole collection to one callback and returns that callback’s result, which can be a number, an array, or another value. The next chained call receives that result instead of the collection. Laravel’s Enumerable API documents pipe() as returning the callback’s result.
$total = collect([1, 2, 3])
->pipe(fn (Collection $items) => $items->sum());
// $total is 6
Use pipe() once the collection has the values you need and you want to calculate or return something from them. After pipe() runs, the chain holds the callback’s result. Here, $total is a number, so you cannot call another collection method on it.
each() to act on every itemeach() calls its callback for every item, passing the item and its key. It returns the collection, so later chained methods receive it. Use it for actions on each item, such as sending each record to a service.
$orders->each(function ($order, $key) use ($invoiceService) {
$invoiceService->send($order);
});
each() does not collect callback return values into a new collection. To change each value and collect the results, use map() instead. For more on choosing collection methods to reshape values, see transforming data with Laravel collection methods.
Laravel’s collection documentation specifies that returning false from an each() callback stops iteration. An arrow function returns its expression’s value, so it also stops iteration when the method it calls returns false. For a callback that only causes side effects, use a block that does not return a value to make its purpose clear.
A chain can use all three methods when each step has a distinct purpose:
$result = collect([1, 2, 3, 4])
->filter(fn (int $value) => $value % 2 === 0)
->tap(function (Collection $items) {
Log::debug('Filtered values', $items->all());
})
->each(function (int $value) use ($processor) {
$processor->process($value);
})
->pipe(fn (Collection $items) => $items->sum());
// $result is 6
filter() keeps the even values. tap() logs the resulting collection, and each() passes each value to the processor. Then pipe() sums the collection and returns the total. Changing the order gives later methods a different value or collection, so check each method’s input and return value when editing the chain. See how method order affects Laravel collection chaining.
These examples describe regular Laravel collections. A LazyCollection reads items as it enumerates them. Laravel documents tapEach() as a separate method that runs its callback as each item is pulled. The LazyCollection API explains how this per-item behavior differs from collection-level tap().
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com