60+ Laravel Collection Methods [In Under 15 Minutes] 🚀
Tags: laravel, php, tutorial, showdev
Published: 2021-07-24 00:22:59

{% user cleancodestudio %}

[![Twitter Follow](https://img.shields.io/twitter/follow/cleancodestudio.svg?style=social)](https://twitter.com/cleancodestudio) 


---

Did you know I have a newsletter? 📬

If you want to get notified when I publish new blog posts or make major project announcements, head over to https://cleancodestudio.paperform.co/




{% youtube rZvxRQmnw18 %}


```php
$collection = collect([
  [
    'id' => 1, 
    'name' => 'john', 
    'age' => 52, 
    'home_owner' => true, 
    'kids' => 4, 
    'type' => User::class
  ],
  [
     'id' => 2, 
     'name' => 'sarah', 
     'age' => 23, 
     'home_owner' => false, 
     'kids' => 1, 
     'type' => User::class
   ],
   [
      'id' => 3, 
      'name' => 'tim', 
      'age' => 28, 
      'home_owner' => false, 
      'kids' => 2, 
      'type' => User::class
   ],
   [
      'id' => 4, 
      'name' => 'sam', 
      'age' => 68, 
      'home_owner' => true, 
      'kids' => 2,  
      'type' => User::class
   ],
   [
      'id' => 5, 
      'name' => 'ray', 
      'age' => 22, 
      'home_owner' => false, 
      'kids' => 2, 
      'type' => User::class
   ],
])
```

## [Max](https://youtu.be/rZvxRQmnw18?t=5)
> _returns the maximum value of a given key_


```php
$collection->max('age');
```

**Output:** `5`


## [min](https://youtu.be/rZvxRQmnw18?t=19)
> _returns the minimum value of a given key_


```php
$collection->min('kids');
```

**Output:** `4`


## [avg and average](https://youtu.be/rZvxRQmnw18?t=27)
> _returns the average value of a given key_


```php
$collection->avg('age');
```

**Output:** `38.6`

```php
$collection->average('age');
```

**Output:** `38.6`


## [median](https://youtu.be/rZvxRQmnw18?t=45)
> _returns the median value of a given key_

```php
$collection->median('id');
```

**Output:** `3`

## [mode](https://youtu.be/rZvxRQmnw18?t=54)
> _returns the mode (most often) value of a given key_

```php
$collection->mode('kids');
```

**Output:** `[0 => 2]`

## [sum](https://youtu.be/rZvxRQmnw18?t=66)
> _returns the sum value of a given key_

```php
$collection->sum('kids');
```

**Output:** `11`

## [sortBy](https://youtu.be/rZvxRQmnw18?t=77)
> _sorts the collection by the given key, keeping the original array keys_

```php
$collection->sortBy('age');
```

**Output:** 
```php
// Collection
[
  [
     'id' => 5, 
     'name' => 'ray', 
     'age' => 22, 
     'home_owner' => false, 
     'kids' => 2, 
     'type' => User::class
  ],
  [
     'id' => 2, 
     'name' => 'sarah', 
     'age' => 23, 
     'home_owner' => false, 
     'kids' => 1, 
     'type' => User::class
   ],
   [
      'id' => 3, 
      'name' => 'tim', 
      'age' => 28, 
      'home_owner' => false, 
      'kids' => 2, 
      'type' => User::class
   ],
   [
     'id' => 1, 
     'name' => 'john', 
     'age' => 52, 
     'home_owner' => true, 
     'kids' => 4, 
     'type' => User::class
   ],
   [
      'id' => 4, 
      'name' => 'sam', 
      'age' => 68, 
      'home_owner' => true, 
      'kids' => 2,  
      'type' => User::class
   ],
]
```

## [sortByDesc](https://youtu.be/rZvxRQmnw18?t=86)
> _sorts the collection descending by the given key, keeping the original array keys_

```php
$collection->sortByDesc('age');
```

**Output:** 
```php
// Collection
[
   [
      'id' => 4, 
      'name' => 'sam', 
      'age' => 68, 
      'home_owner' => true, 
      'kids' => 2,  
      'type' => User::class
   ],
   [
     'id' => 1, 
     'name' => 'john', 
     'age' => 52, 
     'home_owner' => true, 
     'kids' => 4, 
     'type' => User::class
   ],
   [
      'id' => 3, 
      'name' => 'tim', 
      'age' => 28, 
      'home_owner' => false, 
      'kids' => 2, 
      'type' => User::class
   ],
   [
     'id' => 2, 
     'name' => 'sarah', 
     'age' => 23, 
     'home_owner' => false, 
     'kids' => 1, 
     'type' => User::class
  ],
  [
     'id' => 5, 
     'name' => 'ray', 
     'age' => 22, 
     'home_owner' => false, 
     'kids' => 2, 
     'type' => User::class
  ],
]
```

## [nth](https://youtu.be/rZvxRQmnw18?t=98)
> _creates a new collection consisting of every n-th element:_

```php
$collection->nth(2);
```

**Output**
```php
// Collection
[
  [
    'id' => 1, 
    'name' => 'john', 
    'age' => 52, 
    'home_owner' => true, 
    'kids' => 4, 
    'type' => User::class
  ],
  [
     'id' => 3, 
     'name' => 'tim', 
     'age' => 28, 
     'home_owner' => false, 
     'kids' => 2, 
     'type' => User::class
  ],
  [
     'id' => 5, 
     'name' => 'ray', 
     'age' => 22, 
     'home_owner' => false, 
     'kids' => 2, 
     'type' => User::class
  ]
]
```

## [first](https://youtu.be/rZvxRQmnw18?t=124)
> _retrieves the first element of the collection or optionally the first element that meets the condition from a callback function_

```php
$collection->first();
```

**Output:**
```php
  [
    'id' => 1, 
    'name' => 'john', 
    'age' => 52, 
    'home_owner' => true, 
    'kids' => 4, 
    'type' => User::class
  ]
``` 

```php
$collection->first(element => element->id > 1);
```

**Output:**
```php
  [
     'id' => 2, 
     'name' => 'sarah', 
     'age' => 23, 
     'home_owner' => false, 
     'kids' => 1, 
     'type' => User::class
   ],
``` 


## [last](https://youtu.be/rZvxRQmnw18?t=124)
> _retrieves the last element of the collection or optionally the last element that meets the condition from a callback function_

```php
$collection->last();
```

**Output:**
```php
   [
      'id' => 5, 
      'name' => 'ray', 
      'age' => 22, 
      'home_owner' => false, 
      'kids' => 2, 
      'type' => User::class
   ]

``` 

```php
$collection->last(element => element->id < 5);
```

**Output:**
```php
   [
      'id' => 4, 
      'name' => 'sam', 
      'age' => 68, 
      'home_owner' => true, 
      'kids' => 2,  
      'type' => User::class
   ]
``` 

{% youtube rZvxRQmnw18 %}



[Laravel From The Ground Up](https://www.cleancode.studio/series/laravel-from-the-ground-up)





---


{% user cleancodestudio %}


---

Did you know I have a newsletter? 📬

If you want to get notified when I publish new blog posts or make major project announcements, head over to https://cleancodestudio.paperform.co/

{% codepen https://codepen.io/zhorton34/pen/BaRPYdB %}