Eloquent changes to serialize and casts #39850
Unanswered
DarkGhostHunter
asked this question in
Ideas
Replies: 1 comment 2 replies
-
It's not clear to me what problem you are attempting to solve with this. You say models are getting very cumbersome to maintain, but you do not provide any example to back up that claim. Is this a "you" problem, or a problem a bunch of users are having? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently the Eloquent Model it's getting very cumbersome
to maintainto solve the problem of mutability, given the approach of mutators and getters.My proposition is to separate attribute casting and attribute serializing, making it a two-way layer, allowing for more granular control of what interacts with the database.
bool
,int
,string
. If it's anarray
, then it should replace or update the array of attributes being serialized.Mutable
contract will automatically "cache" any object returned to make it mutable, so$model->collection->push('foo')
can work.This makes the
attributes
array become a representation of the truth, and the serializer the responsible of altering the truth so the database receive correct values. The serializer runs only when retrieving/saving.It should be noted that a model should have a
$serializers
property to serialize attributes to the database. For example, decryption/encryption would be a serializer, while a Carbon object would be a Caster. You could mix both to encrypt a datetime.In any case, this would break a lot of code in Laravel, so this kind of implementation should be tentative for Laravel 10.
Beta Was this translation helpful? Give feedback.
All reactions