17
17
The ` Illuminate\Support\Collection ` class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We'll use the ` collect ` helper to create a new collection instance from the array, run the ` strtoupper ` function on each element, and then remove all empty elements:
18
18
19
19
``` php
20
- $collection = collect(['taylor ', 'abigail ', null])->map(function (?string $name) {
20
+ $collection = collect(['Taylor ', 'Abigail ', null])->map(function (?string $name) {
21
21
return strtoupper($name);
22
22
})->reject(function (string $name) {
23
23
return empty($name);
@@ -1152,17 +1152,17 @@ The `flatten` method flattens a multi-dimensional collection into a single dimen
1152
1152
1153
1153
``` php
1154
1154
$collection = collect([
1155
- 'name' => 'taylor ',
1155
+ 'name' => 'Taylor ',
1156
1156
'languages' => [
1157
- 'php ', 'javascript '
1157
+ 'PHP ', 'JavaScript '
1158
1158
]
1159
1159
]);
1160
1160
1161
1161
$flattened = $collection->flatten();
1162
1162
1163
1163
$flattened->all();
1164
1164
1165
- // ['taylor ', 'php ', 'javascript '];
1165
+ // ['Taylor ', 'PHP ', 'JavaScript '];
1166
1166
```
1167
1167
1168
1168
If necessary, you may pass the ` flatten ` method a "depth" argument:
@@ -1203,13 +1203,13 @@ In this example, calling `flatten` without providing the depth would have also f
1203
1203
The ` flip ` method swaps the collection's keys with their corresponding values:
1204
1204
1205
1205
``` php
1206
- $collection = collect(['name' => 'taylor ', 'framework' => 'laravel ']);
1206
+ $collection = collect(['name' => 'Taylor ', 'framework' => 'Laravel ']);
1207
1207
1208
1208
$flipped = $collection->flip();
1209
1209
1210
1210
$flipped->all();
1211
1211
1212
- // ['taylor ' => 'name', 'laravel ' => 'framework']
1212
+ // ['Taylor ' => 'name', 'Laravel ' => 'framework']
1213
1213
```
1214
1214
1215
1215
<a name =" method-forget " ></a >
@@ -1218,12 +1218,12 @@ $flipped->all();
1218
1218
The ` forget ` method removes an item from the collection by its key:
1219
1219
1220
1220
``` php
1221
- $collection = collect(['name' => 'taylor ', 'framework' => 'laravel ']);
1221
+ $collection = collect(['name' => 'Taylor ', 'framework' => 'Laravel ']);
1222
1222
1223
1223
// Forget a single key...
1224
1224
$collection->forget('name');
1225
1225
1226
- // ['framework' => 'laravel ']
1226
+ // ['framework' => 'Laravel ']
1227
1227
1228
1228
// Forget multiple keys...
1229
1229
$collection->forget(['name', 'framework']);
@@ -1272,17 +1272,17 @@ $collection = Collection::fromJson($json);
1272
1272
The ` get ` method returns the item at a given key. If the key does not exist, ` null ` is returned:
1273
1273
1274
1274
``` php
1275
- $collection = collect(['name' => 'taylor ', 'framework' => 'laravel ']);
1275
+ $collection = collect(['name' => 'Taylor ', 'framework' => 'Laravel ']);
1276
1276
1277
1277
$value = $collection->get('name');
1278
1278
1279
- // taylor
1279
+ // Taylor
1280
1280
```
1281
1281
1282
1282
You may optionally pass a default value as the second argument:
1283
1283
1284
1284
``` php
1285
- $collection = collect(['name' => 'taylor ', 'framework' => 'laravel ']);
1285
+ $collection = collect(['name' => 'Taylor ', 'framework' => 'Laravel ']);
1286
1286
1287
1287
$value = $collection->get('age', 34);
1288
1288
@@ -3651,20 +3651,20 @@ For the inverse of `whenEmpty`, see the [whenNotEmpty](#method-whennotempty) met
3651
3651
The ` whenNotEmpty ` method will execute the given callback when the collection is not empty:
3652
3652
3653
3653
``` php
3654
- $collection = collect(['michael ', 'tom ']);
3654
+ $collection = collect(['Michael ', 'Tom ']);
3655
3655
3656
3656
$collection->whenNotEmpty(function (Collection $collection) {
3657
- return $collection->push('adam ');
3657
+ return $collection->push('Adam ');
3658
3658
});
3659
3659
3660
3660
$collection->all();
3661
3661
3662
- // ['michael ', 'tom ', 'adam ']
3662
+ // ['Michael ', 'Tom ', 'Adam ']
3663
3663
3664
3664
$collection = collect();
3665
3665
3666
3666
$collection->whenNotEmpty(function (Collection $collection) {
3667
- return $collection->push('adam ');
3667
+ return $collection->push('Adam ');
3668
3668
});
3669
3669
3670
3670
$collection->all();
@@ -3678,14 +3678,14 @@ A second closure may be passed to the `whenNotEmpty` method that will be execute
3678
3678
$collection = collect();
3679
3679
3680
3680
$collection->whenNotEmpty(function (Collection $collection) {
3681
- return $collection->push('adam ');
3681
+ return $collection->push('Adam ');
3682
3682
}, function (Collection $collection) {
3683
- return $collection->push('taylor ');
3683
+ return $collection->push('Taylor ');
3684
3684
});
3685
3685
3686
3686
$collection->all();
3687
3687
3688
- // ['taylor ']
3688
+ // ['Taylor ']
3689
3689
```
3690
3690
3691
3691
For the inverse of ` whenNotEmpty ` , see the [ whenEmpty] ( #method-whenempty ) method.
0 commit comments