Dynamic Model Attribute Mutators #50759
Unanswered
tylernathanreed
asked this question in
Ideas
Replies: 1 comment 1 reply
-
Please take a look at this part of your PR: if ($method->getNumberOfRequiredParameters() > 0) {
return false;
} An attribute function, even without a constructor, needs return an empty attr. It seems that part caused some failed tests. |
Beta Was this translation helpful? Give feedback.
1 reply
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.
-
I came across a use-case where I wanted a model to have a series of magic attributes based on an array. In this scenario, I had a
preferences
json object, but I wanted the values in this object to be accessible as if they were properties on the model itself. I had a fixed list of allowable preference attribute names, so it seemed possible.However, when I tried to implement this, I came into a snag that I felt could be handled better. If I wanted, say, 3 different preferences, I would have to add the names of these preferences as methods to the model. Sure, I could do that as a trait, but now, if I wanted to add another preference type, I needed to update both my list of allowed preferences, and my magic attribute methods.
I played around with the internals of Laravel, and ended up creating something I'm dubbing "Dynamic Model Attribute Mutators". Essentially, I can programmatically add new attributes to a model:
It's possible that there's more applications for something like this, but it seems like a neat idea.
I've created PR #50770 as an attempt to implement this idea.
Beta Was this translation helpful? Give feedback.
All reactions