Replies: 1 comment
-
As you can see, it is enough to check first parent not all parents. class_parents should be replaced with get_parent_class if (\is_string($parent = \get_parent_class(static::class)) && \method_exists($parent, '__callStatic')) {
/** @phpstan-ignore class.noParent, staticMethod.notFound */
return $parent::__callStatic($method, $parameters);
}
} if (\is_string($parent = \get_parent_class(static::class)) && \method_exists($parent, '__call')) {
/** @phpstan-ignore class.noParent, staticMethod.notFound */
return parent::__call($method, $parameters);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Laravel Version
10.x to master
PHP Version
8.x
Database Driver & Version
No response
Description
Hello,
The
Macroable
trait does not work if used in a class that also uses__call
or__callStatic
magic methods.That's because when either
__call
or__callStatic
is called it assumes that it is because of a registeredmacro
. If not found it will throw an error instead of passing the call upstream in (e.g.)parent::_call[Static]
.I created a very small project to demonstrate this problem at https://github.com/rsd/macroable-test.
This problem affects, but is not limited to
Eloquent Models
.One could argue that
resolveRelationUsing()
could be used. However,resolveRelationUsing()
does not take any arguments. If your method needs one or more argumentsmacro()
is the only way.Keeping with the
Eloquent
scenario, one reason to want to use this way instead of having it all in theModel
is to be able to break optional dependable Models into different Laravel Modules or packages.For example:
Students
model has all general information for students in a College.Different Colleges could need other models to expand
Students
information depending on the majors it offers, likeEngineer
,Physics
,Arts
.So, different Colleges would install the modules / packages it needs.
Otherwise, it would need all relationships to be hardcoded into the
Students
Model breaking modularity.I have already created PRs for:
Note that the solution is barely 3 lines per method.
Steps To Reproduce
1 - Create a Model like:
2 - Register a
macro()
at a ServiceProvider like:Call it like:
Macroable will throw an exception for
create
orhydrate
.Beta Was this translation helpful? Give feedback.
All reactions