-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Enums are not compatible with Collection::value()
#53676
Comments
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! |
While trying to draft a pull request I ran into the following. I delved a little deeper into it and it looks like the issue lies in framework/src/Illuminate/Collections/Traits/EnumeratesValues.php Lines 1082 to 1086 in e7b12ff
The closure that framework/src/Illuminate/Collections/Traits/EnumeratesValues.php Lines 1101 to 1103 in e7b12ff
Enums are objects, if (count($strings) < 2 && count(array_filter([$retrieved, $value], function ($v) {
return is_object($v) && ! $v instanceof UnitEnum;
})) == 1) {
return in_array($operator, ['!=', '<>', '!==']);
} However, we run into a strange issue now. Any ideas on how to proceed from here? |
If I have understood correctly, you want to receive all results from the collection as an array and the enums should not be skipped. In your second example, the enum is skipped because the value is null. framework/src/Illuminate/Collections/Traits/EnumeratesValues.php Lines 317 to 335 in e7b12ff
framework/src/Illuminate/Collections/Traits/EnumeratesValues.php Lines 1096 to 1100 in e7b12ff
This check fails. Simply because method_exists('__toString') cannot be satisfied. In your previous check, you have no values that return the result true. Therefore, the result is also null . If you want to get all results back as an array, try that: collect([
['id' => 1, 'type' => MyEnum::MyCase],
])->select('type')->toArray();
collect([
['id' => 1, 'type' => MyEnum::MyCase],
['id' => 2, 'type' => 'b'],
])->select('type')->toArray();
/**
* Result:
*
* array(1) {
* [0]=> array(1) {
* ["type"]=> enum(App\Enums\MyEnum::MyCase)
* }
* }
*
* array(2) {
* [0]=> array(1) {
* ["type"]=> enum(App\Enums\MyEnum::MyCase)
* }
* [1]=> array(1) {
* ["type"]=> string(1) "b"
* }
* }
*/ |
…nd `value()` Fixes #53676 Signed-off-by: Mior Muhammad Zaki <[email protected]>
…nd `value()` (#53777) * [11.x] Improves `Collection` support for enums using `firstWhere()` and `value()` Fixes #53676 Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
…nd `value()` (laravel#53777) * [11.x] Improves `Collection` support for enums using `firstWhere()` and `value()` Fixes laravel#53676 Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
Laravel Version
11.34.0
PHP Version
8.4.1
Database Driver & Version
No response
Description
When trying to retrieve a value with
Collection::value()
and the expected value is an enum it is skipped.Steps To Reproduce
The text was updated successfully, but these errors were encountered: