-
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
Passing value to class castable attribute #51950
Comments
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 |
Hi @BenjaminMINK. I don't think this works like in your example. Can you try that and let me know if it works? |
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. |
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 $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);
}); |
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! |
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. |
@Lakshan-Madushanka is correct. It doesn't seem we support this usage sorry. |
Laravel Version
10.x
PHP Version
8.3
Database Driver & Version
No response
Description
Hello,
I would like to report that seems to me an issue when passing value to a class castable attribute.
Let's say a model has an Enum Collection attribute
countries
:After fetching the model, we can have something like :
countries: ['FRA', 'USA']
But if we want to manually use the
castAttribute
method with a custom value the result will always be the same as fetched before:Shouldn't we get our passing value as a result?
In the https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php#L31, the
$value
argument is never used, shouldn't be used if passed to the method? Is it by design?The same thing can be observed on the other casting classes.
Thank you!
Steps To Reproduce
Pass a custom value to model's method
castAttribute
, result will always be the data retrieved before:The text was updated successfully, but these errors were encountered: