DEV Community

Cover image for 60+ Laravel Collection Methods [In Under 15 Minutes] πŸš€
Clean Code Studio
Clean Code Studio

Posted on β€’ Edited on

16 6

60+ Laravel Collection Methods [In Under 15 Minutes] πŸš€

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

first

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

$collection->first();
Enter fullscreen mode Exit fullscreen mode

Output:

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

Output:

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

last

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

$collection->last();
Enter fullscreen mode Exit fullscreen mode

Output:

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

Enter fullscreen mode Exit fullscreen mode
$collection->last(element => element->id < 5);
Enter fullscreen mode Exit fullscreen mode

Output:

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

Laravel From The Ground Up



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/

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
cleancodestudio profile image
Clean Code Studio β€’

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

πŸ‘₯ Ideal for solo developers, teams, and cross-company projects

Learn more