Use the parent's morph class if parent returns children#33
Use the parent's morph class if parent returns children#33calebporzio merged 3 commits intotighten:masterfrom wfeller:morphable
Conversation
|
Thanks for the good work on this! |
|
Also, I'm guessing you left this in there by accident, but I'm just curious. What do you use ".idea" files for? Just a scratch pad for ideas? |
|
.idea directory is used by all JetBrains' IDEs (PHPStorm, WebStorm) to store project configuration files. |
|
gotcha, will leave it in then |
|
Also will tag a release today |
|
I'm having a hard time wrapping my head around this. @wfeller, can you explain why the conditional is necessary here?: public function getMorphClass()
{
if ($this->parentHasHasChildrenTrait() && in_array(static::class, $this->getChildTypes())) {
return (new $this->getParentClass())->getMorphClass();
}
return parent::getMorphClass();
}I think my brain is blocked on this for some reason. Why is "(new $this->getParentClass())->get.." any different than "parent::getMorphClass()"? |
|
Hey @calebporzio, Calling You actually got me wondering about the Using the examples in the tests, that means that public function getMorphClass()
{
if ($this->parentHasHasChildrenTrait() /*&& in_array(static::class, $this->getChildTypes())*/) {
// This returns Vehicle::class
return (new $this->getParentClass())->getMorphClass();
}
// And this returns Car::class
return parent::getMorphClass();
} |
|
Got it, thanks for the clarification. Also, great - I'm going to remove the "&&" part of the conditional. I'm just doing some more cleanup on your contribution and I'll have something up to be tagged shortly. |
If the parent is designed to return children, then the children will use the parent's morph class.
Probably solves #10
Without this change, using whereHas doesn't always work on children classes. Unless you do something like this:
instead of just this:
PS: could you tag a new release ?
Thanks