We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Chunks the values from a collection into groups as long the given callback is true. If the optional parameter $preserveKeys as true is passed, it will preserve the original keys.
Get the following consecutive neighbours in a collection from a given chunk size. If the optional parameter $preserveKeys as true is passed, it will preserve the original keys.
Retrieve the first item using the callable given as the first parameter. If no value exists, push the value of the second
parameter into the collection. You can pass a callable as the second parameter.
This method is really useful when dealing with cached class properties, where you want to store a value retrieved from an API or computationally expensive function in a collection to be used multiple times.
Similar to groupBy, but groups the collection by an Eloquent model. Since the key is an object instead of an integer or string, the results are divided into separate arrays.
$if, $then and $else can be any type. If a closure is passed to any of these parameters, then that closure will be executed and the macro will use its results.
When $if returns a truthy value, then $then will be returned, otherwise $else will be returned.
When a closure is passed to $if, $then or $else, the entire collection will be passed as an argument to that closure.
// the `then` closure will be executed// the first element of the returned collection now contains "THIS IS THE VALUE"$collection = collect(['this is a value'])
->if(
fn(Collection$collection) => $collection->contains('this is a value'),
then: fn(Collection$collection) => $collection->map(fn(string$item) => strtoupper($item)),
else: fn(Collection$collection) => $collection->map(fn(string$item) => Str::kebab($item))
);
// the `else` closure will be executed// the first element of the returned collection now contains "this-is-another-value"$collection = collect(['this is another value'])
->if(
fn(Collection$collection) => $collection->contains('this is a value'),
then: fn(Collection$collection) => $collection->map(fn(string$item) => strtoupper($item)),
else: fn(Collection$collection) => $collection->map(fn(string$item) => Str::kebab($item))
);
ifAny
Executes the passed callable if the collection isn't empty. The entire collection will be returned.
collect()->ifAny(function(Collection$collection) { // empty collection so this won't get calledecho'Hello';
});
collect([1, 2, 3])->ifAny(function(Collection$collection) { // non-empty collection so this will get calledecho'Hello';
});
ifEmpty
Executes the passed callable if the collection is empty. The entire collection will be returned.
collect()->ifEmpty(function(Collection$collection) { // empty collection so this will calledecho'Hello';
});
collect([1, 2, 3])->ifEmpty(function(Collection$collection) { // non-empty collection so this won't get calledecho'Hello';
});
insertAfter
Inserts an item after the first occurrence of a given item and returns the updated Collection instance.
Optionally a key can be given.
请发表评论