@@ -157,9 +157,13 @@ For the remainder of this documentation, we'll discuss each method available on
157
157
[ unique] ( #method-unique )
158
158
[ uniqueStrict] ( #method-uniquestrict )
159
159
[ unless] ( #method-unless )
160
+ [ unlessEmpty] ( #method-unlessempty )
161
+ [ unlessNotEmpty] ( #method-unlessnotempty )
160
162
[ unwrap] ( #method-unwrap )
161
163
[ values] ( #method-values )
162
164
[ when] ( #method-when )
165
+ [ whenEmpty] ( #method-whenempty )
166
+ [ whenNotEmpty] ( #method-whennotempty )
163
167
[ where] ( #method-where )
164
168
[ whereStrict] ( #method-wherestrict )
165
169
[ whereIn] ( #method-wherein )
@@ -1926,6 +1930,16 @@ The `unless` method will execute the given callback unless the first argument gi
1926
1930
1927
1931
For the inverse of ` unless ` , see the [ ` when ` ] ( #method-when ) method.
1928
1932
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
+
1929
1943
<a name =" method-unwrap " ></a >
1930
1944
#### ` unwrap() ` {#collection-method}
1931
1945
@@ -1985,6 +1999,88 @@ The `when` method will execute the given callback when the first argument given
1985
1999
1986
2000
For the inverse of ` when ` , see the [ ` unless ` ] ( #method-unless ) method.
1987
2001
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
+
1988
2084
<a name =" method-where " ></a >
1989
2085
#### ` where() ` {#collection-method}
1990
2086
0 commit comments