Repository navigation
Replies: 1 comment 2 replies
I don't see the reason for the above conditional. I imagine this distinction to be quite unintentional. The container can resolve classes with arguments just fine. If we were to change this, I'd say, we do it all the time, not just sometimes. |
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.
Hey everyone! I'd like to discuss a small but annoying limitation in how Eloquent resolves custom cast classes.
The problem
When you register a custom cast in $casts, Laravel instantiates it like this (line 1882 in HasAttributes):
The arguments here are just primitive strings that you pass through a colon in $casts:
No container. This means that if your cast needs, for example, a repository, you can't just declare it in the constructor. You have to write app() directly inside the get() or set() method, which hides the dependency and complicates testing.
What I suggest.
One line in resolveCasterClass():
Laravel already resolves dependencies through a container in Jobs, Listeners, Policies, and so on. Castes break out of this pattern for no apparent reason.
Backward compatibility
Full. If there are string arguments in $casts, the behavior does not change. The only case that changes is when there are no arguments.
What do you think? Are there any reasons why it's intentionally not done that way?
All reactions