Home > PHP Framework > Laravel > A simple understanding of Laravel collections

A simple understanding of Laravel collections

不言
Release: 2018-10-22 14:22:31
forward
3199 people have browsed it

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]);
Copy after login

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);
Copy after login

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]
Copy after login

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]]
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Reserved for collection
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
Why not talk about list collection sorting?
From 1970-01-01 08:00:00
0
0
0
Retrieve documents in a nested collection
From 1970-01-01 08:00:00
0
0
0
mongoose how to get all collection
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template