Categories: Laravel

5 Best Laravel Collections Methods

Share your learning

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.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago