You may need to know about the laravel set


You may need to know about the laravel set


Set throughIlluminate\Support\Collection For example, most of the parameters of laravel’s kernel are passed by collections, but this does not mean that collections are good. As a fast and elegant development framework, laravel has a certain reason, not because of its routing, DB, listener and so on. When you need to deal with a set of arrays, you may need it to help you quickly solve practical problems.

Create collection

$collection = collect([1, 2, 3]);

Obviously, this is a very simple operation, please stop. If you want to say “this operation is very complicated”, it is more similar to the declaration of early PHP5. X version.

$collection = array(1,2,3);

Laravel didn’t do anything complicated for collection, which will be in the next chapter “collection of laravel source code parsing”. Thank you

Return to the original

If you want to convert a collection into data, it’s also very easy to use

collect([1, 2, 3])->all();
[1, 2, 3]

You can use the laravel set for performance considerations. After all, it will help you do 90% of the work of array operations.
For example, we need to divide an array into two or more arrays by a horizontal line. You can use sets to do ~

$collection = collect([1, 2, 3, 4, 5, 6, 7]);

$chunks = $collection->chunk(4);


// [[1, 2, 3, 4], [5, 6, 7]]

And some methods are designed according to the query mode of SQL statements. Let’s take a look at the specific methods.

Method list

Here is a list of some common collection operation methods. Please operate the official for details and all.

method notes
all Return the set to the prototype
average & avg Calculate the average
chunk Splits a collection into small collections of a specified size
collapse To combine a collection of arrays into a collection of arrays
combine You can combine the values of one set as keys and the values of another array or set as values into a set
concat Appends the given array or collection value to the end of the collection
contains Determines whether the set contains a given entry
count Returns the total number of items in the collection
dd Prints the items of the collection and ends the script execution
diff Compare the set with other sets or pure PHP arrays, and then return the values that exist in the original set but do not exist in the given set
each Iterate over the contents of the collection and pass it to the callback function
filter Use the given callback function to filter the contents of the collection, leaving only those that pass the given real test
first Returns the first element in the collection that passes a given real test
groupBy Groups the items in the collection according to the given key
push Adds the given value to the end of the collection
put Sets the given key value pair within the collection
sortBy Sorts the collection by the given key. The sorted collection retains the original array key
where Filter the collection by the given key value


Thank you for reading here. I hope this article can help you. Thank you. Why don’t you practice the assembly?