-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] Add before
and after
methods to Collection
#51752
Conversation
Interesting - when did you find yourself needing these methods? |
@taylorotwell I needed these methods in a couple of scenarios. $previousItem = $items->get($items->search($currentItem) - 1);
$nextItem = $items->get($items->search($currentItem) + 1); It can be written like this using the $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); |
|
||
$position = $this->keys()->search($key); | ||
|
||
if ($position === 0) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
I'm a little late to the party here, but my interpretation of More aptly named functions would have been
should be :
|
This PR introduces two new methods to the
Collection
andLazyCollection
.before
methodThe
before
method returns the item before the given item. It returns null if the given item is the first item or not found.after
methodThe
after
method returns the item after the given item. It returns null if the given item is the last item or not found.