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.
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.
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:
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
The following method used to check the Laravel collection for value existence.
Contains()
Response Type: Boolean
$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
$collection = collect([ ['fruit'=>'apple','amount'=>234], ['fruit'=>'banana','amount'=>243], ['fruit'=>'grapse','amount'=>499] ]); dump($collection->contains('grapse'));
The output will be:
False
$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
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.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…