This paper introduces the usage of larravel framework collection. For your reference, the details are as follows:
Illuminate\Support\Collection For example, most of the parameters passed by laravel kernel use the collection, but this does not mean that the collection is good. His framework, such as rapid and elegant, does not necessarily make sense. When you need to deal with a set of arrays, you may need it to help you solve practical problems quickly.
$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 method of PHP5. X.
$collection = array(1,2,3);
Laravel does not do anything complicated for collection. We will see the next chapter “collection of laravel source code analysis”. Thank you
Back to the prototype
If you want to convert a collection to data, it is also very simple to use
collect([1, 2, 3])->all(); ------> [1, 2, 3]
You can use the laravel collection when it comes to performance, because it will help you do 90 percent of your array operations.
For example, we need to split the array into two or more arrays through a horizontal line. Using the set can be done with soy sauce purple
$collection = collect([1, 2, 3, 4, 5, 6, 7]); $chunks = $collection->chunk(4); $chunks->toArray(); // [[1, 2, 3, 4], [5, 6, 7]]
And some of them are designed according to the query method of SQL statements. Let’s take a look at the specific ones.
Here are some common set operation methods, please operate the official for details and all.
|all||Return the collection to the prototype|
|average & avg||Calculate the average value|
|chunk||Splits a collection into multiple small collections of a specified size|
|collapse||Merge a collection of multiple arrays into a collection of one array|
|combine||You can combine the values of one set as a key and the values of another array or set as values to form a set|
|concat||Appends the given array or collection value to the end of the collection|
|contains||Determines whether the collection contains the given entry|
|count||Returns the total number of items in the collection|
|dd||Print the items for the collection and end script execution|
|diff||Compares the values of a collection with other collections or pure PHP arrays, and returns values that exist in the original collection but not in the given collection|
|each||Iterates the contents of the collection and passes 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 items within a set based on a given key|
|push||Adds the given value to the end of the collection|
|put||Sets the given key value pair in the collection|
|sortBy||Sorts the set by a given key. The sorted collection retains the original array key|
|where||Filter the collection by a given key value|
More interested readers can view the special topics of this website: introduction and advanced course of larravel framework, summary of excellent development framework of PHP, introduction course of PHP object-oriented programming, introduction course of PHP + MySQL database operation and summary of common database operation skills of PHP
I hope that this article will be helpful to the PHP program design based on larravel framework.