Skip to content

Commit d2f4fdd

Browse files
authored
Merge pull request #5955 from jasonmccreary/collection-until
[7.x] Document until collection method
2 parents 2cdc358 + 4b4457b commit d2f4fdd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

collections.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ For the remainder of this documentation, we'll discuss each method available on
175175
[unless](#method-unless)
176176
[unlessEmpty](#method-unlessempty)
177177
[unlessNotEmpty](#method-unlessnotempty)
178+
[until](#method-until)
178179
[unwrap](#method-unwrap)
179180
[values](#method-values)
180181
[when](#method-when)
@@ -2150,6 +2151,33 @@ Alias for the [`whenNotEmpty`](#method-whennotempty) method.
21502151

21512152
Alias for the [`whenEmpty`](#method-whenempty) method.
21522153

2154+
<a name="method-until"></a>
2155+
#### `until()` {#collection-method}
2156+
2157+
The `until` method returns items in the collection until the given value is found:
2158+
2159+
$collection = collect([1, 2, 3, 4]);
2160+
2161+
$subset = $collection->until(3);
2162+
2163+
$subset->all();
2164+
2165+
// [1, 2]
2166+
2167+
You may also pass a callback to the `until` method to perform your own logic. The callback should return `true` when the `until` method should stop.
2168+
2169+
$collection = collect([1, 2, 3, 4]);
2170+
2171+
$subset = $collection->until(function ($item) {
2172+
return $item >= 3;
2173+
});
2174+
2175+
$subset->all();
2176+
2177+
// [1, 2]
2178+
2179+
If the given value is not found or callback does not return `true`, the `until` method will return all items in the collection.
2180+
21532181
<a name="method-unwrap"></a>
21542182
#### `unwrap()` {#collection-method}
21552183

@@ -2552,7 +2580,7 @@ The `zip` method merges together the values of the given array with the values o
25522580
<a name="higher-order-messages"></a>
25532581
## Higher Order Messages
25542582

2555-
Collections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. The collection methods that provide higher order messages are: [`average`](#method-average), [`avg`](#method-avg), [`contains`](#method-contains), [`each`](#method-each), [`every`](#method-every), [`filter`](#method-filter), [`first`](#method-first), [`flatMap`](#method-flatmap), [`groupBy`](#method-groupby), [`keyBy`](#method-keyby), [`map`](#method-map), [`max`](#method-max), [`min`](#method-min), [`partition`](#method-partition), [`reject`](#method-reject), [`some`](#method-some), [`sortBy`](#method-sortby), [`sortByDesc`](#method-sortbydesc), [`sum`](#method-sum), and [`unique`](#method-unique).
2583+
Collections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. The collection methods that provide higher order messages are: [`average`](#method-average), [`avg`](#method-avg), [`contains`](#method-contains), [`each`](#method-each), [`every`](#method-every), [`filter`](#method-filter), [`first`](#method-first), [`flatMap`](#method-flatmap), [`groupBy`](#method-groupby), [`keyBy`](#method-keyby), [`map`](#method-map), [`max`](#method-max), [`min`](#method-min), [`partition`](#method-partition), [`reject`](#method-reject), [`some`](#method-some), [`sortBy`](#method-sortby), [`sortByDesc`](#method-sortbydesc), [`sum`](#method-sum), [`unique`](#method-unique), and [`until`](#method-until).
25562584

25572585
Each higher order message can be accessed as a dynamic property on a collection instance. For instance, let's use the `each` higher order message to call a method on each object within a collection:
25582586

0 commit comments

Comments
 (0)