Home > Article > PHP Framework > A simple understanding of Laravel collections
This article brings you a simple understanding of Laravel collections. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Preface
Collections are instantiated through Illuminate\Database\Eloquent\Collection. Laravel’s kernel uses collections for most parameter transfers, but this does not It doesn't mean that collection is good. Laravel is a fast and elegant development framework for a certain reason, not because of its routing, DB, listeners, etc. When you need to process a set of arrays, you may need it to help you solve practical problems quickly.
Create a collection
$collection = collect([1, 2, 3]);
Obviously, this is a very simple operation. Please stop if you want to say "this operation is complicated", it is more similar to The declaration method of the early PHP5.x version.
$collection = array(1,2,3);
Laravel does not do anything complicated for collection.
Return to the prototype
If you want to convert the collection into data, its use is also very simple
collect([1, 2, 3])->all(); ------> [1, 2, 3]
But when considering performance In this case, you can use Laravel collections, after all, it will help you complete 90% of the work of array operations.
For example, we need to cut the array through a horizontal line and divide it into 2 or more arrays. You can use collections to do it~
$collection = collect([1, 2, 3, 4, 5, 6, 7]); $chunks = $collection->chunk(4); $chunks->toArray(); // [[1, 2, 3, 4], [5, 6, 7]]
And some methods are designed based on the query method of SQL statements. Let’s take a look at the specific ones.
Method List
Here are some commonly used collection operation methods. Please refer to the official website for details and complete details.
Method | Comments |
---|---|
all | Will set Return to the prototype |
average & avg | Calculate the average |
chunk | Split the set into Multiple small collections of specified sizes |
collapse | Merge multiple collections of arrays into one collection of arrays |
combine | You can use the value of a set as a "key", and then combine the value of another array or set as a "value" into a set |
concat | Append the given array or collection value to the end of the collection |
contains | Determine whether the collection contains the given item |
count | Return the total number of items in the collection |
dd | Print the items of the collection and end script execution |
diff | Compare the values of a collection with other collections or pure PHP arrays, and then return the values that exist in the original collection but do not exist in the given collection |
each | Iterate over the contents of the collection and pass it into the callback function |
filter | Use the given The callback function filters the contents of the collection, leaving only those that pass the given real test |
first | Returns the first element in the collection that passes the given real test |
groupBy | Group the items within the collection based on 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 keys |
where | Filter the collection by the given key value |
The above is the detailed content of A simple understanding of Laravel collections. For more information, please follow other related articles on the PHP Chinese website!