pulli/laravel-collection-macros

Package for some custom collection macros.

Fund package maintenance!
the-pulli

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/pulli/laravel-collection-macros

v1.0.0 2025-12-28 17:32 UTC

This package is auto-updated.

Last update: 2025-12-28 17:48:51 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Contains some handy collection macros.

Installation

You can install the package via composer:

composer require pulli/laravel-collection-macros

You can publish the service provider via:

php artisan vendor:publish --tag="pulli-collection-macros-provider"

You can publish the config file with:

php artisan vendor:publish --tag="pulli-collection-macros-config"

This is the contents of the published config file:

return [
    'auto-update' => true,
];

Usage

Macros

  • mapToCollectionFrom
  • mapToCollection
  • positive
  • recursiveToArrayFrom
  • recursiveToArray

mapToCollectionFrom

Static method: Maps all arrays/objects recursively to a collection object of collections, which allow nested function calling.

$collection = Collection::mapToCollectionFrom([['test' => 1], 2, 3]);

$collection->get(0)->get('test'); // returns 1

// Item has a toArray() public method, then it can be wrapped into a collection like this:
$collection = Collection::mapToCollectionFrom([Item(), Item()], true);

positive

Returns a boolean value, if the collection contains elements or not.

Collection::make([1, 2, 3])->positive() // returns true
Collection::make()->positive() // returns false

recursiveToArrayFrom

Static method: Like mapToCollectionFrom it maps all arrays/objects recursively to an array.

// Item has a toArray() public method, then it can be wrapped into the collection like this:
$array = Collection::recursiveToArrayFrom(['item1' => Item(), 'item2' => Item()]);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.