5 Best Laravel Collections Methods
These methods allow us to modify or retrieve data without re-query to the database. There is a lot of collection methods available and here are 5 best Laravel collections methods.
Merge of two different collections
The below method used to merge two collections and it will produce the new merged collection.
merge()
Response Type: Collection
See full article: How to merge two different Laravel collections.
Collection of multiple arrays into single array collection
The below Laravel collections method used to collapse multiple arrays collection into a single array collection.
collapse()
Response Type: Collection
For example:
$fruits = collect([[‘apple’], [‘banana’], [‘grapse’]]); dd($fruits);
The output will be:
Now use the collapse method to collapse all arrays into single array.
$allFruits = $fruits->collapse(); dump($allFruits->all());
The output will be:
Get the average value from the collection
We can use the following method to calculate the average value, like this:
avg()
Response Type: Integer
public function employees() { $emps = collect([ [ 'id'=> 'ADF345FG', 'salary'=> 13000 ], [ 'id'=> 'ADF346FG', 'salary'=> 9000 ], [ 'id'=> 'ADF347FG', 'salary'=> 20000 ] ]); dump($emps); dump('Average salary is = '.$emps->avg('salary')); }
The output will be:
Average Salary is = 14000
How to check value existence in the Laravel collection?
The following method used to check the Laravel collection for value existence.
Contains()
Response Type: Boolean
Single dimensional array collection
$collection = collect([‘fruit’=>’apple’, ‘amount’=>340]); dd($collection->contains(‘fruit’));
The output will be:
False
Actually contains method assume the single parameter as a value not the key of array. like this,
$collection = collect([‘fruit’=>’apple’, ‘amount’=>340]); dd($sdArray->contains(‘apple’));
The output will be:
True
Multidimensional array collection
$collection = collect([ ['fruit'=>'apple','amount'=>234], ['fruit'=>'banana','amount'=>243], ['fruit'=>'grapse','amount'=>499] ]); dump($collection->contains('grapse'));
The output will be:
False
Search in pairs (Keys and Values)
$collection = collect([‘fruit’=>’apple’, ‘amount’=>340]); or $collection = collect([ ['fruit'=>'apple','amount'=>234], ['fruit'=>'banana','amount'=>243], ['fruit'=>'grapse','amount'=>499] ]); dump($collection->contains(‘fruit’, ‘apple’));
The output will be:
True
How to reverse Laravel collection?
Like array_reverse() in php reverse() method used to reverse Laravel collection.
Response Type: Collection
$fruits = collect([‘apple’, ‘banana’, ‘grapse’, ‘orange’, ‘papaya’]); dump($fruits->reverse());
The output will be:
Collection {#210 ▼ #items: array:5 [▼ 4 => "papaya" 3 => "orange" 2 => "grapse" 1 => "banana" 0 => "apple" ] }
This method reverses the pairs of array-like key and value both.
More Laravel collection methods https://laravel.com/docs/5.8/eloquent-collections#available-methods
That’s it, I will definitely back with more Laravel collections methods.
If you find this article useful, please share and comment it. Trust me, your every share and comment valuable for me.