Skip to content

Commit 1765f2c

Browse files
authored
Merge pull request #4803 from driesvints/missing-collection-method
[5.7] Document whenEmpty and whenNotEmpty methods
2 parents 9764429 + 46f4c48 commit 1765f2c

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

collections.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,13 @@ For the remainder of this documentation, we'll discuss each method available on
157157
[unique](#method-unique)
158158
[uniqueStrict](#method-uniquestrict)
159159
[unless](#method-unless)
160+
[unlessEmpty](#method-unlessempty)
161+
[unlessNotEmpty](#method-unlessnotempty)
160162
[unwrap](#method-unwrap)
161163
[values](#method-values)
162164
[when](#method-when)
165+
[whenEmpty](#method-whenempty)
166+
[whenNotEmpty](#method-whennotempty)
163167
[where](#method-where)
164168
[whereStrict](#method-wherestrict)
165169
[whereIn](#method-wherein)
@@ -1926,6 +1930,16 @@ The `unless` method will execute the given callback unless the first argument gi
19261930

19271931
For the inverse of `unless`, see the [`when`](#method-when) method.
19281932

1933+
<a name="method-unlessempty"></a>
1934+
#### `unlessEmpty()` {#collection-method}
1935+
1936+
Alias for the [`whenNotEmpty`](#method-whennotempty) method.
1937+
1938+
<a name="method-unlessnotempty"></a>
1939+
#### `unlessNotEmpty()` {#collection-method}
1940+
1941+
Alias for the [`whenEmpty`](#method-whenempty) method.
1942+
19291943
<a name="method-unwrap"></a>
19301944
#### `unwrap()` {#collection-method}
19311945

@@ -1985,6 +1999,88 @@ The `when` method will execute the given callback when the first argument given
19851999

19862000
For the inverse of `when`, see the [`unless`](#method-unless) method.
19872001

2002+
<a name="method-whenempty"></a>
2003+
#### `whenEmpty()` {#collection-method}
2004+
2005+
The `whenEmpty` method will execute the given callback when the collection is empty:
2006+
2007+
$collection = collect(['michael', 'tom']);
2008+
2009+
$collection->whenEmpty(function ($collection) {
2010+
return $collection->push('adam');
2011+
});
2012+
2013+
$collection->all();
2014+
2015+
// ['michael', 'tom']
2016+
2017+
2018+
$collection = collect();
2019+
2020+
$collection->whenEmpty(function ($collection) {
2021+
return $collection->push('adam');
2022+
});
2023+
2024+
$collection->all();
2025+
2026+
// ['adam']
2027+
2028+
2029+
$collection = collect(['michael', 'tom']);
2030+
2031+
$collection->whenEmpty(function($collection) {
2032+
return $collection->push('adam');
2033+
}, function($collection) {
2034+
return $collection->push('taylor');
2035+
});
2036+
2037+
$collection->all();
2038+
2039+
// ['michael', 'tom', 'taylor']
2040+
2041+
For the inverse of `whenEmpty`, see the [`whenNotEmpty`](#method-whennotempty) method.
2042+
2043+
<a name="method-whennotempty"></a>
2044+
#### `whenNotEmpty()` {#collection-method}
2045+
2046+
The `whenNotEmpty` method will execute the given callback when the collection is not empty:
2047+
2048+
$collection = collect(['michael', 'tom']);
2049+
2050+
$collection->whenNotEmpty(function ($collection) {
2051+
return $collection->push('adam');
2052+
});
2053+
2054+
$collection->all();
2055+
2056+
// ['michael', 'tom', 'adam']
2057+
2058+
2059+
$collection = collect();
2060+
2061+
$collection->whenNotEmpty(function ($collection) {
2062+
return $collection->push('adam');
2063+
});
2064+
2065+
$collection->all();
2066+
2067+
// []
2068+
2069+
2070+
$collection = collect();
2071+
2072+
$collection->whenNotEmpty(function($collection) {
2073+
return $collection->push('adam');
2074+
}, function($collection) {
2075+
return $collection->push('taylor');
2076+
});
2077+
2078+
$collection->all();
2079+
2080+
// ['taylor']
2081+
2082+
For the inverse of `whenNotEmpty`, see the [`whenEmpty`](#method-whenempty) method.
2083+
19882084
<a name="method-where"></a>
19892085
#### `where()` {#collection-method}
19902086

0 commit comments

Comments
 (0)