->whereBelongsTo()
support for MorphTo
relationships
#53038
-
I've recently started to explore the Instead, I kept getting errors. As it turns out, there's a To address this, I'm considering a potential solution by adding a check to determine if the relationship is a if ($relationship instanceof MorphTo) {
$this->where(fn ($query) => (
$relatedCollection->each(fn (Model $model) => (
$query->orWhere(fn ($innerQuery) => (
// ->getQualifiedMorphTypeColumn() doesn't exist, probably add it to MorphTo relationship?
$innerQuery->where($relationship->getQualifiedMorphTypeColumn(), \get_class($model))
->where($relationship->getQualifiedForeignKeyName(), $model->getKey())
))
))
));
/**
* And the resulting query would be something like
* ... WHERE (
* (qualified_morph_type_column='App\Models\Model' AND qualified_foreign_key_name='id1')
* OR (qualified_morph_type_column='App\Models\OtherModel' AND qualified_foreign_key_name='id2')
* OR ...
* )
*/
} I'm eager to contribute to the Laravel community by refining this functionality. I'd be happy to contribute a pull request to address this issue if it aligns with the project's goals. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Opened a PR for this: #53041 |
Beta Was this translation helpful? Give feedback.
-
Closing this as it turns out there's already a similar method for |
Beta Was this translation helpful? Give feedback.
Closing this as it turns out there's already a similar method for
MorphTo
relationships (->whereMorphedTo()
).