Http://laravelacademy.org/post/178.html#ipt_kb_toc_178_3
1. Introduction
The Illuminate\support\collection class provides a smooth, convenient encapsulation of the processing of array data. For example, looking at the following code, we use the Help function collect to create a new collection instance, run the Strtoupper function for each element, and then remove all empty elements:
$collection = Collect ([' Taylor ', ' Abigail ', NULL])->map (function ($name) {return strtoupper ($name);
} )->reject (function ($name) {return empty ($name);
});
As you can see, the collection class allows you to perform matching and reducing operations on the underlying array using the method chain, usually without a collection method returning a new collection instance. 2. Create a collection
As mentioned above, the Help function collect returns a new Illuminate\support\collection instance for the given array, so it is simple to create the collection:
$collection = Collect ([1, 2, 3]);
By default, a collection of eloquent models always returns collection instances, and, wherever it is, the collection class is freely available to the method. 3. List of collection methods
In the next section of this document we will discuss each of the effective methods on the collection class, all of which can be smoothed in the way of the method chain to manipulate the underlying array. In addition, almost every method returns a new collection instance, allowing you to keep the original collection backup when necessary. All ()
The all method simply returns the underlying array represented by the collection:
Collect ([1, 2, 3])->all ();
[1, 2, 3]
Chunk ()
The chunk method divides a collection into smaller sets of small size:
$collection = Collect ([1, 2, 3, 4, 5, 6, 7]);
$chunks = $collection->chunk (4);
$chunks->toarray ();
[[1, 2, 3, 4], [5, 6, 7]]
This method is especially useful in view when dealing with a fence system such as bootstrap, building you have a collection of eloquent models that you want to display in a fence:
@foreach ($products->chunk (3) as $chunk)
<div class= "Row" >
@foreach ($chunk as $product)
<div class= "col-xs-4" >{{$product->name}}</div>
@endforeach
</div>
@endforeach
collapse ()
The collapse method shrinks a set of multidimensional arrays into a one-dimensional array:
$collection = Collect ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
$collapsed = $collection->collapse ();
$collapsed->all ();
[1, 2, 3, 4, 5, 6, 7, 8, 9]
contains ()
The Contains method determines whether the collection contains a given item:
$collection = Collect ([' Name ' => ' Desk ', ' price ' =>]);
$collection->contains (' Desk ');
True
$collection->contains (' New York ');
False
You can also pass a key-value pair to the Contains method, which will determine whether a given key-value pair exists in the collection:
$collection = Collect ([
[' Product ' => ' Desk ', ' price ' =>],
[' Product ' => ' Chair ', ' price ' => 100],
]);
$collection->contains (' product ', ' bookcase ');
False
Finally, you can also pass a callback to the contains method to perform your own real test:
$collection = Collect ([1, 2, 3, 4, 5]);
$collection->contains (function ($key, $value) {return
$value > 5;
});
False
count ()
The Count method returns the number of all items in the collection:
$collection = Collect ([1, 2, 3, 4]);
$collection->count ();
4
diff ()
The Diff method compares the collection to another collection or native PHP array:
$collection = Collect ([1, 2, 3, 4, 5]);
$diff = $collection->diff ([2, 4, 6, 8]);
$diff->all ();
[1, 3, 5]
Each ()
The each method iterates through the data items in the collection and passes each data item to the given callback:
$collection = $collection->each (function ($item, $key) {
//
});
Callback returns false will terminate the loop:
$collection = $collection->each (function ($item, $key) {
if (/* some condition/*) {return
false;
}
} );
filter ()
The Filter method filters the collection by a given callback, and only the data items that pass a given test are preserved:
$collection = Collect ([1, 2, 3, 4]);
$filtered = $collection->filter (function ($item) {return
$item > 2;
});
$filtered->all ();
[3, 4]
The opposite method of filter is reject. A ()
The first method returns an element that passes through the test collection:
Collect ([1, 2, 3, 4])->first (function ($key, $value) {return
$value > 2;
});
3
You can also call the first method without arguments to get the element of the collection, and return NULL if the collection is empty:
Collect ([1, 2, 3, 4])->first ();
1
Flatten ()
The Flatten method converts a set of multidimensional dimensions into one dimension:
$collection = Collect ([' Name ' => ' Taylor ', ' Languages ' => [' php ', ' JavaScript ']]);
$flattened = $collection->flatten ();
$flattened->all ();
[' Taylor ', ' php ', ' JavaScript '];