10000 [12.x] Add support for callback evaluation in containsOneItem method by fernandokbs · Pull Request #55622 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add support for callback evaluation in containsOneItem method #55622

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

Conversation

fernandokbs
Copy link
Contributor

Why this change?

This PR extends the containsOneItem method to accept an optional callback. This allows users to determine whether exactly one item in the collection satisfies a given condition.

Example usage

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

$collection->containsOneItem(fn($item) => $item === 2); // true
$collection->containsOneItem(fn($item) => $item > 1);   // false (2 and 3 match)

@fernandokbs fernandokbs force-pushed the feature/support-callback-in-containsOneItem branch from e728b73 to b109331 Compare May 1, 2025 16:38
@fernandokbs fernandokbs force-pushed the feature/support-callback-in-containsOneItem branch from b109331 to 516e7b0 Compare May 1, 2025 16:40
@shaedrich
Copy link
Contributor

This can easily be done via $collection->filter(fn($item) => $item === 2)->containsOneItem(); already

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

Successfully merging this pull request may close these issues.

3 participants
0