Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add before and after methods to Collection #9700

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ For the majority of the remaining collection documentation, we'll discuss each m

<div class="collection-method-list" markdown="1">

[after](#method-after)
[all](#method-all)
[average](#method-average)
[avg](#method-avg)
[before](#method-before)
[chunk](#method-chunk)
[chunkWhile](#method-chunkwhile)
[collapse](#method-collapse)
Expand Down Expand Up @@ -255,8 +257,37 @@ For the majority of the remaining collection documentation, we'll discuss each m
}
</style>

<a name="method-after"></a>
#### `after()` {.collection-method .first-collection-method}

The `after` method returns the item after the given item. It returns `null` if the given item is not found or is the last item:

$collection = collect([1, 2, 3, 4, 5]);

$collection->after(3);

// 4

$collection->after(5);

// null

It searches for the given item using a "loose" comparison, meaning a string with an integer value will be considered equal to an integer of the same value. To use "strict" comparison, pass `true` as the second argument to the method:

collect([2, 4, 6, 8])->after('4', $strict = true);

// null

Alternatively, you may provide your own closure to search for the first item that passes a given truth test:

collect([2, 4, 6, 8])->after(function (int $item, int $key) {
return $item > 5;
});

// 8

<a name="method-all"></a>
#### `all()` {.collection-method .first-collection-method}
#### `all()` {.collection-method}

The `all` method returns the underlying array represented by the collection:

Expand Down Expand Up @@ -287,6 +318,31 @@ The `avg` method returns the [average value](https://en.wikipedia.org/wiki/Avera

// 2

<a name="method-before"></a>
#### `before()` {.collection-method}

The `before` method is the opposite of the [`after`](#method-after) method. It returns the item before the given item. It returns `null` if the given item is not found or is the first item:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't cover the strict comparison and closure here since they are mentioned in the after method.


$collection = collect([1, 2, 3, 4, 5]);

$collection->before(3);

// 2

$collection->before(1);

// null

collect([2, 4, 6, 8])->before('4', $strict = true);

// null

collect([2, 4, 6, 8])->before(function (int $item, int $key) {
return $item > 5;
});

// 4

<a name="method-chunk"></a>
#### `chunk()` {.collection-method}

Expand Down