Created
February 13, 2025 10:22
-
-
Save mikehins/38710e35a8a703edc5d972544098f658 to your computer and use it in GitHub Desktop.
Laravel collection helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Collection; | |
$users = collect([ | |
(object) ['name' => 'Alice', 'age' => 25, 'votes' => 5, 'active' => true], | |
(object) ['name' => 'Bob', 'age' => 30, 'votes' => 10, 'active' => false], | |
(object) ['name' => 'Charlie', 'age' => 35, 'votes' => 15, 'active' => true], | |
]); | |
// Get the average of a specific property across all items. | |
$users->average->age; // Output: 30 | |
$users->avg->votes; // Output: 10 | |
// Check if any user meets a certain condition. | |
$users->contains->name == 'Alice'; // Output: true | |
// Loop through each item in the collection. | |
$users->each->activate(); // No direct output, activates each user | |
// Check if all users meet a certain condition. | |
$users->every->active; // Output: false | |
// Filter collection items based on a boolean property. | |
$users->filter->active; // Output: [Alice, Charlie] | |
// Get the first item in the collection that meets a condition. | |
$users->first->active; // Output: Alice | |
// Flatten a nested array of properties into a single collection. | |
$nested = collect([ | |
(object) ['tags' => ['php', 'laravel']], | |
(object) ['tags' => ['javascript', 'vue']] | |
]); | |
$nested->flatMap->tags; // Output: ['php', 'laravel', 'javascript', 'vue'] | |
// Group collection items by a specific property. | |
$users->groupBy->active; // Output: [true => [Alice, Charlie], false => [Bob]] | |
// Create an associative array using a property as the key. | |
$users->keyBy->name; // Output: ['Alice' => Alice, 'Bob' => Bob, 'Charlie' => Charlie] | |
// Transform each item in the collection and return a new collection. | |
$users->map->name; // Output: ['Alice', 'Bob', 'Charlie'] | |
// Find the maximum value of a specific property. | |
$users->max->age; // Output: 35 | |
// Find the minimum value of a specific property. | |
$users->min->age; // Output: 25 | |
// Split collection into two based on a boolean condition. | |
$users->partition->active; // Output: [active => [Alice, Charlie], inactive => [Bob]] | |
// Remove items that match a condition. | |
$users->reject->active; // Output: [Bob] | |
// Skip items until a certain condition is met. | |
$users->skipUntil->active; // Output: [Bob] | |
// Skip items while a certain condition is true. | |
$users->skipWhile->active; // Output: [Bob] | |
// Check if at least one item meets a condition. | |
$users->some->active; // Output: true | |
// Sort collection items by a specific property in ascending order. | |
$users->sortBy->age; // Output: [Alice, Bob, Charlie] | |
// Sort collection items by a specific property in descending order. | |
$users->sortByDesc->age; // Output: [Charlie, Bob, Alice] | |
// Get the sum of a specific property across all items. | |
$users->sum->votes; // Output: 30 | |
// Take items until a condition is met. | |
$users->takeUntil->active; // Output: [Bob] | |
// Take items while a condition remains true. | |
$users->takeWhile->active; // Output: [Alice] | |
// Remove duplicate values based on a specific property. | |
$users->unique->age; // Output: [25, 30, 35] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From this tweet https://x.com/Akr4mKhan/status/1889675836908794263