The article was forwarded from the professional Laravel developer community with the original link: https://learnku.com/laravel/t...
In Laravel, there is a very useful class called a collection of operand arrays.Believe that every Laravel developer has used collections more or less, especially when working with Eloquent.In this article, I will list 10 commonly used methods.
1. Weight removal
The duplicates method retrieves all duplicate values in the collection.The returned array contains keys for each value in the original array.
<?php $collection = collect(['james', 'lisa', 'ryan', 'james', 'brad', 'lisa']); $collection->duplicates(); // [3 => 'james', 5 => 'lisa']
2. Traverse Sets
You can also traverse a collection without using foreach, using the each method.Like the usual foreach method, the each method has two parameters, item and key.
<?php $collection->each(function ($item, $key) { // Do stuff });
3. Debug Output Collection
When we debug code, we often need to output to view the contents of the collection.In addition to using dump($collection), you can also use the dump method of the collection, which has the advantage that the output looks cleaner and cleaner.
<?php $collection = collect(['james', 'lisa', 'ryan']); $collection->dump(); /* Collection { #items: array:3 [ 0 => "james" 1 => "lisa" 2 => "ryan" ] } */
If you want to output the contents of a collection and breakpoints, you can use the dd (Output and Termination) method.
4. Has
The have method can be used to see if a key exists in a collection.The parameter can be either a string or an array.If an array is passed as a parameter, all values in the array must be keys in the collection in order for the result to return true or false.
<?php $collection = collect([ 'title' => 'Harry Potter', 'author' => 'J.K. Rowling', 'price' => 25 ]); $collection->has('author'); // true $collection->has(['title', 'price']); // true $collection->has(['price', 'rating']); // false
5. Implode
The implode method can connect values in a collection.This method works much like the implode method in PHP.
<?php $collection = collect([ ['title' => 'Gift card', 'price' => 50], ['title' => 'Chair', 'price' => 80], ]); $collection->implode('title', ', '); // Gift card, Chair
If the values in the set are just strings or numbers, you simply pass the connector as a parameter.
<?php $collection = collect([1,2,'foo',3,'bar']); $collection->implode('-'); // 1-2-foo-3-bar
6. Push and Pull
You can use the push method to attach items to the end of a collection.If you need to add to the beginning of the collection, you can use the prepend method.
<?php $collection = collect([1, 2, 3]); $collection->push(4); $collection->all(); // [1, 2, 3, 4]
The pull method removes the given key from the collection and returns its corresponding value.
<?php $collection = collect([ 'title' => 'Harry Potter', 'author' => 'J.K. Rowling', 'price' => 25 ]); $collection->pull('author'); // 'J.K. Rowling' $collection->all(); // ['title' => 'Harry Potter', 'price' => 25]
7. Shuffle method
The shuffle method will randomly sort the items in the collection.
<?php $collection = collect([1, 2, 3]); $shuffled = $collection->shuffle(); $shuffled->all(); // [3, 1, 2] (random example)
8. Max method
You can use the max method to get the maximum value in the collection.If the collection contains an array, you can pass a parameter to get the maximum value of a key.
<?php $max = collect([ ['title' => 'Gift card', 'price' => 50], ['title' => 'Chair', 'price' => 80] ])->max('price'); // 80 $max = collect([1, 2, 3])->max(); // 3
9. Filter
You can use the filter method to filter collections.This method accepts a callback function as a parameter.A filter rule is defined in a callback function, and only values that match the rule can be left in the set.
<?php $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $even_numbers = $collection->filter(function ($value, $key) { return $value % 2 == 0; }); $even_numbers->all(); // [2, 4, 6, 8, 10]
10. Slice
The slice method can be used to split the collection, return the index position specified by the parameter and the value thereafter, and the array of returned values will contain the keys of the original array.
Note that in the following example, the parameter passed is 2.This means splitting the collection starting at index 2, not element value 2 in the array.
<?php $collection = collect([1, 2, 3, 4, 5]); $sliced = $collection->slice(2); $sliced->all(); // [3, 4, 5]