60+ Laravel Collection Methods [In Under 15 Minutes]

Posted on

cleancodestudio image

Twitter Follow


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
   ],
])
Enter fullscreen mode

Exit fullscreen mode



Max

returns the maximum value of a given key

$collection->max('age');
Enter fullscreen mode

Exit fullscreen mode

Output: 5



min

returns the minimum value of a given key

$collection->min('kids');
Enter fullscreen mode

Exit fullscreen mode

Output: 4



avg and average

returns the average value of a given key

$collection->avg('age');
Enter fullscreen mode

Exit fullscreen mode

Output: 38.6

$collection->average('age');
Enter fullscreen mode

Exit fullscreen mode

Output: 38.6



median

returns the median value of a given key

$collection->median('id');
Enter fullscreen mode

Exit fullscreen mode

Output: 3



mode

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

$collection->mode('kids');
Enter fullscreen mode

Exit fullscreen mode

Output: [0 => 2]



sum

returns the sum value of a given key

$collection->sum('kids');
Enter fullscreen mode

Exit fullscreen mode

Output: 11



sortBy

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

$collection->sortBy('age');
Enter fullscreen mode

Exit fullscreen mode

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
   ],
]
Enter fullscreen mode

Exit fullscreen mode



sortByDesc

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

$collection->sortByDesc('age');
Enter fullscreen mode

Exit fullscreen mode

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
  ],
]
Enter fullscreen mode

Exit fullscreen mode



nth

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

$collection->nth(2);
Enter fullscreen mode

Exit fullscreen mode

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

Leave a Reply

Your email address will not be published. Required fields are marked *