Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to use owner key if provided to the morph relation #3093

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from

Conversation

alejojperez
Copy link

@alejojperez alejojperez commented Aug 1, 2024

In Laravel Polymorphic relations, you can specify the owner key to the relation

protected function getResultsByType($type)
{
    $instance = $this->createModelByType($type);

    $key = $instance->getKeyName(); // <--- Before the change
    $ownerKey = $this->ownerKey ?? $instance->getKeyName(); // <---- After the change

    $query = $instance->newQuery();

    return $query->whereIn($ownerKey, $this->gatherKeysByType($type, $instance->getKeyType()))->get();
}

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@alejojperez alejojperez requested a review from a team as a code owner August 1, 2024 22:05
@GromNaN GromNaN requested review from GromNaN and removed request for alcaeus August 12, 2024 09:17
@GromNaN
Copy link
Member

GromNaN commented Aug 12, 2024

That sounds correct to me. Could you add a test?

Tracking in PHPORM-226

@GromNaN
Copy link
Member

GromNaN commented Sep 2, 2024

I've tried to reproduce the error, but I can't find the condition in which the code you've fixed is called.

It seems to be with eager loading of relations, but I don't find how. Could you show an example of your model definition and your query?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants