Download the PHP package code-distortion/laravel-collection-macros without Composer
On this page you can find all versions of the php package code-distortion/laravel-collection-macros. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download code-distortion/laravel-collection-macros
More information about code-distortion/laravel-collection-macros
Files in code-distortion/laravel-collection-macros
Package laravel-collection-macros
Short Description A set of useful Laravel collection macros
License MIT
Homepage https://github.com/spatie/laravel-collection-macros
Informations about the package laravel-collection-macros
A set of useful Laravel collection macros
This package is a fork of spatie/laravel-collection-macros. It contains a subset of the original macros as well as a few extra ones.
This package is intended for PERSONAL USE. Please see the original Spatie package if you would like to submit a PR or request a feature.
Installation
Install the package via composer:
The package will automatically register itself.
Macros
Macros kept from the original spatie package:
catch
extract
glob
ifAny
ifEmpty
none
paginate
prioritize
simplePaginate
try
transpose
validate
New macros added:
keepValues
keyedKeys
rejectValues
catch
See Try
extract
Extract keys from a collection. This is very similar to only
, with two key differences:
extract
returns an array of values, not an associative array- If a value doesn't exist, it will fill the value with
null
instead of omitting it
extract
is useful when using PHP 7.1 short list()
syntax.
glob
Returns a collection of a glob()
result.
ifAny
Executes the passed callable if the collection isn't empty. The entire collection will be returned.
ifEmpty
Executes the passed callable if the collection is empty. The entire collection will be returned.
keepValues
Returns a collection containing only values that were in the given list.
keepValues
accepts a second parameter to turn strict-comparisons on (default false).
keyedKeys
Returns a collection where the values are the same as the keys.
none
Checks whether a collection doesn't contain any occurrences of a given item, key-value pair, or passing truth test. The function accepts the same parameters as the contains
collection method.
paginate
Create a LengthAwarePaginator
instance for the items in the collection.
This paginates the contents of $posts
with 5 items per page. paginate
accepts quite some options, head over to the Laravel docs for an in-depth guide.
prioritize
Move elements to the start of the collection.
rejectValues
Removes the given values from the collection.
rejectValues
accepts a second parameter to turn strict-comparisons on (default false).
simplePaginate
Create a Paginator
instance for the items in the collection.
This paginates the contents of $posts
with 5 items per page. simplePaginate
accepts quite some options, head over to the Laravel docs for an in-depth guide.
For a in-depth guide on pagination, check out the Laravel docs.
try
If any of the methods between try
and catch
throw an exception, then the exception can be handled in catch
.
While the methods are named try
/catch
for familiarity with PHP, the collection itself behaves more like a database transaction. So when an exception is thrown, the original collection (before the try) is returned.
You may gain access to the collection within catch by adding a second parameter to your handler. You may also manipulate the collection within catch by returning a value.
transpose
The goal of transpose is to rotate a multidimensional array, turning the rows into columns and the columns into rows.
validate
Returns true
if the given $callback
returns true for every item. If $callback
is a string or an array, regard it as a validation rule.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
Contributing
Contributions are not being sought for this package. Please see the original spatie/laravel-collection-macros package if you would like to contribute.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Freek Van der Herten
- Sebastian De Deyne
- Tim Chandler (fork)
- All Contributors
License
The MIT License (MIT). Please see License File for more information.