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

[12.x] Adds firstValue() to Collection #55289

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Apr 6, 2025

What?

This PR enables allows to return a value from the first element given the value is not null.

use App\Models\User;

$users = User::query()->limit(10)->get();

// before
$name = $users->firstWhere(function (User $user) {
    return $user->premium_since->isFuture();
})?->name

// after
$name = $users->firstValue(function (User $user) {
    if ($user->premium_since->isFuture()) {
        return $user->name;
    }
})

The idea of this PR is to avoid adding a ?->something at the end of the firstWhere, because sometimes adding a small tail to a code like this may be ignored by mistake.

@taylorotwell
Copy link
Member

The "before" looks kinda fine to me? 😅

@DarkGhostHunter
Copy link
Contributor Author

The "before" looks kinda fine to me? 😅

Are you sure? 🙃

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.

2 participants