-
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
[9.x] Conditionable::when returns Collection when callback or default return empty value (e.g. 0) #40877
Comments
I just tried the example out in Tinkerwell Web, it correctly returns
|
Seems like this is more of a feature request as this behavior is unchanged from 8.x? You mention: |
Yes, this happened after the upgrade to v9 yesterday. |
@spaceemotion it's just odd to me because besides the changes from above PR it's the exact same code. |
It seems like it is because of #37632. The code in v8.x does not have the |
@spaceemotion you're correct! Can you send in a PR? |
I can try this afternoon 👍 |
I think the reason I sent this PR to 9.x was because of this change. |
Yeah, if you read #37632, I called this behavior out specifically. The reason for the change was to unify the behavior of I agree with @spaceemotion that using the null coalesce operator would be preferable—I just used the ternary shorthand because that was what is used everywhere else in the framework. |
Can one of you send in a PR? |
@inxilpro Are you already working on this? If not, I could try it later. |
Yeah, I can push up a PR. |
Description:
I have the following snippet that calculates a number of serviced hours based on a condition:
In case the calculator returns 0 hours (or the entries are empty) the expected result would be
0
.However, the function returns the collection itself, as the
Conditionable
trait returns$this
when the return value is "empty":framework/src/Illuminate/Conditionable/Traits/Conditionable.php
Lines 28 to 32 in ca33630
The same logic applies to the unless function as well.
A possible solution is to use
??
instead of?:
. Not sure if this is a breaking change though (as it has worked before).Steps To Reproduce:
The text was updated successfully, but these errors were encountered: