60+ Laravel Collection Methods [In Under 15 Minutes] 🚀

cleancodestudio image

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/


$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
   ],
])

returns the maximum value of a given key

$collection->max('age');

Output: 5

returns the minimum value of a given key

$collection->min('kids');

Output: 4

returns the average value of a given key

$collection->avg('age');

Output: 38.6

$collection->average('age');

Output: 38.6

returns the median value of a given key

$collection->median('id');

Output: 3

returns the mode (most often) value of a given key

$collection->mode('kids');

Output: [0 => 2]

returns the sum value of a given key

$collection->sum('kids');

Output: 11

sorts the collection by the given key, keeping the original array keys

$collection->sortBy('age');

Output:

// 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
   ],
]

sorts the collection descending by the given key, keeping the original array keys

$collection->sortByDesc('age');

Output:

// 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
  ],
]

creates a new collection consisting of every n-th element:

$collection->nth(2);

Output

// 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
  ]
]

retrieves the first element of the collection or optionally the first element that meets the condition from a callback function

$collection->first();

Output:

[
    'id' => 1, 
    'name' => 'john', 
    'age' => 52, 
    'home_owner' => true, 
    'kids' => 4, 
    'type' => User::class
  ]
$collection->first(element => element->id > 1);

Output:

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

retrieves the last element of the collection or optionally the last element that meets the condition from a callback function

$collection->last();

Output:

[
      'id' => 5, 
      'name' => 'ray', 
      'age' => 22, 
      'home_owner' => false, 
      'kids' => 2, 
      'type' => User::class
   ]
$collection->last(element => element->id < 5);

Output:

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

cleancodestudio image

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/

20