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 #51752

Merged
merged 10 commits into from
Jun 11, 2024

Conversation

avosalmon
Copy link
Contributor

This PR introduces two new methods to the Collection and LazyCollection.

before method

The before method returns the item before the given item. It returns null if the given item is the first item or not found.

$collection = collect([1, 2, 3, 4, 5, 'name' => 'taylor', 'framework' => 'laravel']);

$collection->before(2) // 1
$collection->before('taylor') // 5
$collection->before('laravel') // 'taylor'
$collection->before(fn ($value) => $value > 4) // 4
$collection->before(fn ($value) => ! is_numeric($value)) // 5
$collection->before(1) // null
$collection->before('not found') // null

after method

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

$collection = collect([1, 2, 3, 4, 5, 'name' => 'taylor', 'framework' => 'laravel']);

$collection->after(1) // 2
$collection->after('taylor') // 'laravel'
$collection->after(fn ($value) => $value > 4) // 'taylor'
$collection->after(fn ($value) => ! is_numeric($value)) // 'laravel'
$collection->after('laravel') // null
$collection->after('not found') // null

@avosalmon avosalmon marked this pull request as draft June 10, 2024 04:58
@avosalmon avosalmon marked this pull request as ready for review June 10, 2024 05:54
@taylorotwell
Copy link
Member

Interesting - when did you find yourself needing these methods?

@avosalmon
Copy link
Contributor Author

avosalmon commented Jun 11, 2024

@taylorotwell I needed these methods in a couple of scenarios.
For example, when I was building a page that displayed a single resource while allowing the user to navigate between the next and previous resources, I wrote something like this.

$previousItem = $items->get($items->search($currentItem) - 1);
$nextItem = $items->get($items->search($currentItem) + 1);

It can be written like this using the before and after methods.

$previousItem = $items->before($currentItem);
$nextItem = $items->after($currentItem);

Another use case was that I retrieved a previous date from a series of dates.

- $previousDate = $dates->get($dates->search($date) - 1);
- $nextDate = $dates->get($dates->search($date) + 1);
+ $previousDate = $dates->before($date);
+ $nextDate = $dates->after($date);

@taylorotwell taylorotwell merged commit 7aef671 into laravel:11.x Jun 11, 2024
28 checks passed

$position = $this->keys()->search($key);

if ($position === 0) {

This comment was marked as resolved.

@chrislurty
Copy link

I'm a little late to the party here, but my interpretation of before and after would mean they would return the collection of items before/after the given item index, instead if just the adjacent item.

More aptly named functions would have been previous and next, which are the terms used in the original example.

$previousItem = $items->before($currentItem);
$nextItem = $items->after($currentItem);

should be :

$previousItem = $items->previous($currentItem);
$nextItem = $items->next($currentItem);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants