Comments (7)
Sorry if I'm unclear, what I would expect by passing a custom value is that the castable class can return correct casting for any passed value
from framework.
Hi @BenjaminMINK. I don't think this works like in your example. AsEnumCollection
is currently documented to work like follow: https://laravel.com/docs/11.x/eloquent-mutators#casting-arrays-of-enums
Can you try that and let me know if it works?
from framework.
Hello,
Thank you for your answer, but what I've showed in my example is for Laravel 10.x.
I've also tested with the new syntax in Laravel 11.x, the same "issue" happens, in fact, the casting classes haven't changed since 10.x, so the same thing happens.
from framework.
Based on AsEnumCollection class:
As today, the get method has:
$data = Json::decode($attributes[$key]);
if (! is_array($data)) {
return;
}
$enumClass = $this->arguments[0];
return (new Collection($data))->map(function ($value) use ($enumClass) {
return is_subclass_of($enumClass, BackedEnum::class)
? $enumClass::from($value)
: constant($enumClass.'::'.$value);
});
We might be expect something like this when we pass the $value
argument:
$data = Json::decode($value ?? $attributes[$key]);
if (! is_array($data)) {
return;
}
$enumClass = $this->arguments[0];
return (new Collection($data))->map(function ($value) use ($enumClass) {
return is_subclass_of($enumClass, BackedEnum::class)
? $enumClass::from($value)
: constant($enumClass.'::'.$value);
});
from framework.
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!
from framework.
castAttrbute() is an internal method since its visibility is protected. However, I'm curious how you managed to call that method from outside without an exception being thrown.
from framework.
@Lakshan-Madushanka is correct. It doesn't seem we support this usage sorry.
from framework.
Related Issues (20)
- Eager Loading One-to-Many Relationship in Laravel Returns Empty Collection Despite Data Being Present HOT 20
- Can't exclude route from CSRF validation when route is defined in `then` HOT 5
- "Model::preventAccessingMissingAttributes()" Causes Exception During Pagination with ResourceCollection HOT 6
- Upgrade to 11.16.0 still has bootstrap/app.php from 10.0 HOT 4
- When you are trying to change column type to float (with default precision) - column type will be DOUBLE instead.
- Output from the artisan queue:listener is colorless HOT 1
- Broken pipe errno=32 after upgrade to 11.16.0 HOT 1
- Model create time out HOT 1
- Laravel 11 framework config auto-merging is a risky breaking change HOT 9
- MariaDB uuid migration fieldType HOT 3
- Bug: `Attribute "..." cannot target parameter (allowed targets: property)` HOT 6
- Route model binding fails when additional route parameters are present HOT 6
- Error when acessing an Attribute from another Attribute HOT 1
- Nullable validation rule stop further validation for empty string HOT 3
- casts()-Method is not working HOT 3
- [11.17] Laravel attempts to load attributes it didn't previously HOT 1
- with function from eloquent builder does not work with callback and multiple relations HOT 3
- Issue with declaring `bindings` and `singletons` properties HOT 2
- Artisan about command error HOT 1
- $this->attempts() function is not working after upgrade laravel 11 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from framework.