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

.ltag__user__id__386677 .follow-action-button {
background-color: #339d15 !important;
color: #ffffff !important;
border-color: #339d15 !important;
}

Clean Code StudioFollow


This content originally appeared on DEV Community and was authored by Clean Code Studio

.ltag__user__id__386677 .follow-action-button { background-color: #339d15 !important; color: #ffffff !important; border-color: #339d15 !important; }
cleancodestudio image

$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

returns the maximum value of a given key

$collection->max('age');

Output: 5

min

returns the minimum value of a given key

$collection->min('kids');

Output: 4

avg and average

returns the average value of a given key

$collection->avg('age');

Output: 38.6

$collection->average('age');

Output: 38.6

median

returns the median value of a given key

$collection->median('id');

Output: 3

mode

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

$collection->mode('kids');

Output: [0 => 2]

sum

returns the sum value of a given key

$collection->sum('kids');

Output: 11

sortBy

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

sortByDesc

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

nth

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

first

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

last

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
   ]

Laravel From The Ground Up


This content originally appeared on DEV Community and was authored by Clean Code Studio


Print Share Comment Cite Upload Translate Updates
APA

Clean Code Studio | Sciencx (2021-07-24T00:22:59+00:00) 60+ Laravel Collection Methods [In Under 15 Minutes] ?. Retrieved from https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/

MLA
" » 60+ Laravel Collection Methods [In Under 15 Minutes] ?." Clean Code Studio | Sciencx - Saturday July 24, 2021, https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/
HARVARD
Clean Code Studio | Sciencx Saturday July 24, 2021 » 60+ Laravel Collection Methods [In Under 15 Minutes] ?., viewed ,<https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/>
VANCOUVER
Clean Code Studio | Sciencx - » 60+ Laravel Collection Methods [In Under 15 Minutes] ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/
CHICAGO
" » 60+ Laravel Collection Methods [In Under 15 Minutes] ?." Clean Code Studio | Sciencx - Accessed . https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/
IEEE
" » 60+ Laravel Collection Methods [In Under 15 Minutes] ?." Clean Code Studio | Sciencx [Online]. Available: https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» 60+ Laravel Collection Methods [In Under 15 Minutes] ? | Clean Code Studio | Sciencx | https://www.scien.cx/2021/07/24/60-laravel-collection-methods-in-under-15-minutes-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.