werxe/laravel-collection-macros is a Laravel package for custom laravel collection macros..
It currently has 26 GitHub stars and 26.887 downloads on Packagist (latest version v8.0.0).
Install it with composer require werxe/laravel-collection-macros.
Discover more Laravel packages by werxe
or browse all Laravel packages to compare alternatives.
Last updated
Custom Laravel Collection Macros for Laravel 7+.
This package is compliant with the FIG standards PSR-1, PSR-2 and PSR-4 to ensure a high level of interoperability between shared PHP. If you notice any compliance oversights, please send a patch via pull request.
Version | Laravel | PHP Version ------- | ------- | ------------ 4.x | 7.x | >= 7.2.5 3.x | 6.x | >= 7.2 2.x | 5.8.x | >= 7.1.3 1.x | 5.7.x | >= 7.1.3
Install the package via Composer by running:
composer require werxe/laravel-collection-macros
By default all macros are enabled and the macro name is the lower cased version of the macro class.
If you want to customize which macros are enabled or just rename the macro name, you can do so by publishing the configuration file, by running:
php artisan vendor:publish --tag="werxe:collection-macros.config"
The configuration file is now published at config/werxe/collection-macros/config.php.
incrementIncrement a value that's inside a Collection
$collection = collect([
'total' => 1,
]);
$collection->increment('total', 2); // 3
decrementDecrement a value that's inside a Collection
$collection = collect([
'total' => 3,
]);
$collection->decrement('total', 2); // 1
ksortSorts the Collection by its keys.
$collection = collect(['d' => 'lemon', 'a' => 'orange', 'b' => 'banana', 'c' => 'apple']);
$collection->ksort(); // ['a' => 'orange', 'b' => 'banana', 'c' => 'apple', 'd' => 'lemon']
krsortSorts the Collection by its keys in the reverse order
$collection = collect(['d' => 'lemon', 'a' => 'orange', 'b' => 'banana', 'c' => 'apple']);
$collection->krsort(); // ['d' => 'lemon', 'c' => 'apple', 'b' => 'banana', 'a' => 'orange']
recursiveRecursively convert nested arrays into Laravel Collections.
$collection = collect([
'name' => 'John Doe',
'emails' => [
'[email protected]',
'[email protected]',
],
'contacts' => [
[
'name' => 'Richard Tea',
'emails' => [
'[email protected]',
],
],
],
]);
// Convert the nested arrays into Collections
$convertedCollection = $collection->recursive();
// Get the contacts as a Collection
$contacts = $convertedCollection->get('contacts');
Thank you for your interest in Laravel Collection Macros. Here are some of the many ways to contribute.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Laravel Collection Macros is licenced under the MIT License (MIT). Please see the license file for more information.