-
Notifications
You must be signed in to change notification settings - Fork 11.3k
The value collection method shouldn't consider 0 as null #54910
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
Comments
The issue comes from this line
$value is true in this case.
Dangers of using == instead of ===... |
I think changing this would become a potential breaking change |
Use |
On the method: framework/src/Illuminate/Collections/Traits/EnumeratesValues.php Lines 331 to 338 in 4284e61
Changing the call to the You can test it out using a macro: Artisan::command('local:test', function () {
Collection::macro('myValue', function ($key, $default = null) {
if ($value = $this->first()) {
return data_get($value, $key, $default);
}
return value($default);
});
$collection = collect([
["name" => "Tim", "balance" => 0],
["name" => "John", "balance" => 200],
]);
dump(
$collection->value("balance"), // 200
$collection->myValue("balance"), // 0
);
}); But I doubt they would make this change on a patch release. The You could:
The docs' description actually is over-simplified on the original PR's description. Reference: https://laravel.com/docs/12.x/collections#method-value And from what is currently on the docs, I'd also expect the behavior you described. Maybe @stevebauman, who authored the PR which added the |
Hey guys (thanks for ping @rodrigopedra)! Yea I would personally agree and consider this a bug. I would think it should return the first value where the key exists -- even if its value is null, so it operates like the Eloquent query method. Oversight on my part, my mistake! Hard to say if this would be a breaking change since this would be the expected behavior imo! |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
@phadaphunk are you sure the first implementation requirement was to return the value from first element even if not set? Because as it is now, it returns the first value that is loosely true by looking at elements starting from first element. |
@macropay-solutions From the Documentation, it sure seems like this is what the intended behaviour was
You're right to point that out tho. I think it should look for the first that has the matching key and return the value regardless of it being falsy or even null. I'll adjust. |
Maybe this can solve this issue: #55512 |
Laravel Version
12.x
PHP Version
8.3.15
Database Driver & Version
No response
Description
From the documentation on the
value()
collection method0 Should not be treated as null so the check should be specifically against
null
.I would expect to get Tim's debt based on the doc.
Even more so in this case,
Same goes for all values that evaluate the same way.
Or even a "0" string.
Steps To Reproduce
Run this code
In a scenario like this, skipping a user because his balance is 0 feels wrong. The access pattern might not be the best but it's just an example of a real use case.
I know it is probably handled that way because of how PHP handles zero values but I feel this is the kind of gotcha that can be confusing to new developers using Laravel.
The text was updated successfully, but these errors were encountered: