-
-
Notifications
You must be signed in to change notification settings - Fork 163
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
Why jsonSerialize() does not return toArray() anymore? #257
Comments
I stand by #244 (comment)
|
In that case, is it still possible to cast the object into an array, a json or a string? That's for sure a breaking change for the next major version, but I think having changed For my particular case on a projet, in order to restaure my previous behaviour (return an array of the object), i've created the following trait : <?php
namespace App\Enums;
trait BenSampoEnumArrayable
{
public function toArray(): mixed
{
return $this;
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
} |
This just saved me from hours of banging my head against the wall. Thanks! |
Closing, as I no longer plan to develop this library further - see #332. |
@spawnia Hi,
I want to talk about this change 5094099#diff-ee71af26064ea012953831df497c2297ea91606aa26bb133e73dc1b51deec2ccR603
which was discussed here #244
I think this change must be documented, at leat, as it breaks the behavior explained in the documentation of the "Customising
$model->toArray()
behavior" https://github.com/BenSampo/laravel-enum#customising-model-toarray-behaviourLaravel for json responses uses
jsonSerialize()
, which previously usedtoArray()
returned. Now both functions are independent, and for array AND json, we need to customize the return.Personally, I think this makes more sense to revert to the behavior of
jsonSerialize()
which returns thetoArray()
function.The text was updated successfully, but these errors were encountered: