“This is the second day of my participation in the First Challenge 2022, for more details: First Challenge 2022”.

First of all, why should I edit this article?

Here’s a little chestnut:

$collection->toArray()
Copy the code

The above function to convert a collection to an array is the most commonly used command for my project. It’s funny to think about how I use toArray() to convert data to an array as well, Eloquent.

Collection has already done so many good packages for us. I went back to the array.

If that happens to you, then this article is for you.

The Laravel collection is a very useful tool in the Laravel framework. Laravel collections are like arrays in PHP, and collections are designed to make it easy to manipulate arrays. The data that Laravel finds is generally in the form of collections.

This is an introduction to advanced knowledge.

Test related commands

Filter the collection with the given callback function, leaving only those items that pass the judgment test:

$filtered = $collection->filter(function ($item) {
    return $item > 2;
});
Copy the code

Returns the first element in the collection to pass the given test:

$res = collect([1, 2, 3, 4, 5])->first(function ($key, $value) {
    var_dump('key:' . $key);
    var_dump('value:' . $value);
    return $key > 2;
});
dd($res);
Copy the code

The output is:

string(5) "key:1"
string(7) "value:0"
string(5) "key:2"
string(7) "value:1"
string(5) "key:3"
string(7) "value:2"
3
Copy the code

Returns the last element in the collection to pass the given test:

$collection->last();
Copy the code

Convert command

Convert the collection to a pure PHP array:

This is my most commonly used command before, I encapsulated the basic function, the collection is converted into an array to use, too funny ~ learning technology or in-depth wow.

$collection->toArray();
Copy the code

Convert the collection to JSON:

$collection->toJson();
Copy the code

Convert a multidimensional set to a one-dimensional set:

$flattened = $collection->flatten();
Copy the code

Example code:

$collection = collect ([1, 2, 3, 'num = > [4 and 6]]). var_dump($collection); $collection = $collection->flatten(); var_dump($collection); dd('-----');Copy the code

Running results:

object(Illuminate\Support\Collection)#2541 (1) { ["items":protected]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3)  ["num"]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } } } object(Illuminate\Support\Collection)#32 (1) { ["items":protected]=> array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) } } "-- -- -- -- --"Copy the code

Interchangekeys and corresponding values in the set:

$flipped = $collection->flip();
Copy the code

Example code:

$collection = collect(['one'=>1]);
print $collection;
print PHP_EOL;
$collection = $collection->flip();
print $collection;
print PHP_EOL;
exit();
Copy the code

{“one”:1} {“1″:”one”}

Advanced command

To remove an item from the collection by key name:

$collection->forget('name');
Copy the code

Returns a new collection containing items that can be used to display at the given page number:

Useful in CMS and projects where page numbers need to be displayed

$chunk = $collection->forPage(2, 3);
Copy the code

Returns the item for the given key. If the key does not exist, null is returned:

It’s very basic and needs no introduction

$value = $collection->get('name');
Copy the code

Group items within a given key substitution set:

In some cases, we can group the data directly during SQL query. When SQL group query is not convenient, we can get the data first and then group the data by groupBy() of the collection

$grouped = $collection->groupBy('account_id');
Copy the code

Welcome to the interactive

Do you have any useful commands to discuss in the comments section

Related articles recommended

Laravel Collection Base edition.

Hardcore articles recommended

Rethinking performance optimization: reduce DB queries and use member variables properly.

PHP to Go mid 2021 summary

How do I receive an interface error at the first time? No need to test the girl to question whether you are not dead interface.

Git use actual combat: collaborative development of many people, emergency repair online bug Git operation guide.

Performance tuning reflection: Do not manipulate DB in a for loop

Performance tuning reflection: Do not operate on DB advanced versions in for loops

The last

👍🏻 : feel the harvest please point a praise to encourage!

🌟 : Collect articles, easy to look back!

💬 : Comment exchange, mutual progress!