@@ -1829,6 +1829,149 @@ <h6>Miscellaneous</h6>
1829
1829
// Returns the value it is given
1830
1830
$value = with(new Foo)-> work();
1831
1831
</ pre >
1832
+ </ section >
1833
+ < section class ="cmd-description grid-item ">
1834
+ < h4 > < a name ="collection " href ="#collection "> Collection</ a > < a href ="https://laravel.com/docs/5.1/collections " title ="Collections CLI @ Laravel Docs "> < i class ="icon-file-text "> </ i > </ a > </ h4 >
1835
+ < pre class ="prettyprint lang-php ">
1836
+ // Creating Collections
1837
+ collect([1, 2, 3]);
1838
+ // Simply returns the underlying array represented by the collection:
1839
+ $collection-> all();
1840
+ // Returns the average of all items in the collection:
1841
+ $collection-> avg();
1842
+ // Breaks the collection into multiple, smaller collections of a given size:
1843
+ $collection-> chunk(4);
1844
+ // Collapses a collection of arrays into a flat collection:
1845
+ $collection-> collapse();
1846
+ // Determines whether the collection contains a given item:
1847
+ $collection-> contains('New York');
1848
+ // Returns the total number of items in the collection:
1849
+ $collection-> count();
1850
+ // Iterates over the items in the collection and passes each item to a given callback:
1851
+ $collection = $collection-> each(function ($item, $key) {
1852
+ });
1853
+ // Creates a new collection consisting of every n-th element:
1854
+ $collection-> every(4);
1855
+ // Pass offset as the second argument:
1856
+ $collection-> every(4, 1);
1857
+ // Returns all items in the collection except for those with the specified keys:
1858
+ $collection-> except(['price', 'discount']);
1859
+ // Filters the collection by a given callback:
1860
+ $filtered = $collection-> filter(function ($item) {
1861
+ return $item > 2;
1862
+ });
1863
+ // Returns the first element in the collection that passes a given truth test:
1864
+ collect([1, 2, 3, 4])-> first(function ($key, $value) {
1865
+ return $value > 2;
1866
+ });
1867
+ // Flattens a multi-dimensional collection into a single dimension:
1868
+ $flattened = $collection-> flatten();
1869
+ // Swaps the collection's keys with their corresponding values:
1870
+ $flipped = $collection-> flip();
1871
+ // Removes an item from the collection by its key:
1872
+ $collection-> forget('name');
1873
+ // Returns a new collection containing the items:
1874
+ $chunk = $collection-> forPage(2, 3);
1875
+ // Returns the item at a given key. If the key does not exist, null is returned:
1876
+ $value = $collection-> get('name');
1877
+ // Groups the collection's items by a given key:
1878
+ $grouped = $collection-> groupBy('account_id');
1879
+ // Determines if a given key exists in the collection:
1880
+ $collection-> has('email');
1881
+ // Joins the items in a collection:
1882
+ $collection-> implode('product', ', ');
1883
+ // Removes any values that are not present in the given array or collection:
1884
+ $intersect = $collection-> intersect(['Desk', 'Chair', 'Bookcase']);
1885
+ // Returns true if the collection is empty:
1886
+ collect([])-> isEmpty();
1887
+ // Keys the collection by the given key:
1888
+ $keyed = $collection-> keyBy('product_id');
1889
+ // Pass a callback, which should return the value to key the collection by:
1890
+ $keyed = $collection-> keyBy(function ($item) {
1891
+ return strtoupper($item['product_id']);
1892
+ });
1893
+ // Returns all of the collection's keys:
1894
+ $keys = $collection-> keys();
1895
+ // Returns the last element in the collection:
1896
+ $collection-> last();
1897
+ // Iterates through the collection and passes each value to the given callback:
1898
+ $multiplied = $collection-> map(function ($item, $key) {
1899
+ return $item * 2;
1900
+ });
1901
+ // Return the maximum value of a given key:
1902
+ $max = collect([['foo' => 10], ['foo' => 20]])-> max('foo');
1903
+ $max = collect([1, 2, 3, 4, 5])-> max();
1904
+ // Merges the given array into the collection:
1905
+ $merged = $collection-> merge(['price' => 100, 'discount' => false]);
1906
+ // Return the minimum value of a given key:
1907
+ $min = collect([['foo' => 10], ['foo' => 20]])-> min('foo');
1908
+ $min = collect([1, 2, 3, 4, 5])-> min();
1909
+ // Returns the items in the collection with the specified keys:
1910
+ $filtered = $collection-> only(['product_id', 'name']);
1911
+ // Retrieves all of the collection values for a given key:
1912
+ $plucked = $collection-> pluck('name');
1913
+ // Removes and returns the last item from the collection:
1914
+ $collection-> pop();
1915
+ // Adds an item to the beginning of the collection:
1916
+ $collection-> prepend(0);
1917
+ // Pass a second argument to set the key of the prepended item:
1918
+ $collection-> prepend(0, 'zero');
1919
+ // Removes and returns an item from the collection by its key:
1920
+ $collection-> pull('name');
1921
+ // Appends an item to the end of the collection:
1922
+ $collection-> push(5);
1923
+ // Sets the given key and value in the collection:
1924
+ $collection-> put('price', 100);
1925
+ // Returns a random item from the collection:
1926
+ $collection-> random();
1927
+ // Pass an integer to random. If that integer is more than 1, a collection of items is returned:
1928
+ $random = $collection-> random(3);
1929
+ // Reduces the collection to a single value:
1930
+ $total = $collection-> reduce(function ($carry, $item) {
1931
+ return $carry + $item;
1932
+ });
1933
+ // Filters the collection using the given callback:
1934
+ $filtered = $collection-> reject(function ($item) {
1935
+ return $item > 2;
1936
+ });
1937
+ // Reverses the order of the collection's items:
1938
+ $reversed = $collection-> reverse();
1939
+ // Searches the collection for the given value and returns its key if found:
1940
+ $collection-> search(4);
1941
+ // Removes and returns the first item from the collection:
1942
+ $collection-> shift();
1943
+ // Randomly shuffles the items in the collection:
1944
+ $shuffled = $collection-> shuffle();
1945
+ // Returns a slice of the collection starting at the given index:
1946
+ $slice = $collection-> slice(4);
1947
+ // Sorts the collection:
1948
+ $sorted = $collection-> sort();
1949
+ // Sorts the collection by the given key:
1950
+ $sorted = $collection-> sortBy('price');
1951
+ // Removes and returns a slice of items starting at the specified index:
1952
+ $chunk = $collection-> splice(2);
1953
+ // Returns the sum of all items in the collection:
1954
+ collect([1, 2, 3, 4, 5])-> sum();
1955
+ // Returns a new collection with the specified number of items:
1956
+ $chunk = $collection-> take(3);
1957
+ // Converts the collection into a plain PHP array:
1958
+ $collection-> toArray();
1959
+ // Converts the collection into JSON:
1960
+ $collection-> toJson();
1961
+ // Iterates over the collection:
1962
+ $collection-> transform(function ($item, $key) {
1963
+ return $item * 2;
1964
+ });
1965
+ // Returns all of the unique items in the collection:
1966
+ $unique = $collection-> unique();
1967
+ // Returns a new collection with the keys reset to consecutive integers:
1968
+ $values = $collection-> values();
1969
+ // Filters the collection by a given key / value pair:
1970
+ $filtered = $collection-> where('price', 100);
1971
+ // Merges together the values of the given array with the values of the collection:
1972
+ $zipped = $collection-> zip([100, 200]);
1973
+ </ pre >
1974
+ </ section >
1832
1975
</ div >
1833
1976
</ div >
1834
1977
</ div >
0 commit comments