8000 QueryToMany, QueryToOne, HasMany, HasOne, BelongsTo and BelongsToMany may fail to find a relation if declared with resolveRelationUsing · Issue #288 · laravel-json-api/laravel · GitHub
[go: up one dir, main page]

Skip to content
QueryToMany, QueryToOne, HasMany, HasOne, BelongsTo and BelongsToMany may fail to find a relation if declared with resolveRelationUsing #288
Closed
@danilogiacomi

Description

@danilogiacomi

If you add a relation to a model via the Model::resolveRelationUsing() method (https://laravel.com/docs/11.x/eloquent-relationships#dynamic-relationships) the getRelation() methods in QueryToMany, QueryToOne, HasMany, HasOne, BelongsTo and BelongsToMany don't recognize it as they use method_exists to check if it's available, whereas the relations created this way are only available to the instance and when checked with relationResolver

So for instance in QueryToMany.php

`
/**
* @return EloquentRelation
*/
private function getRelation(): EloquentRelation
{
$name = $this->relation->relationName();

    assert(method_exists($this->model, $name), sprintf(
        'Expecting method %s to exist on model %s',
        $name,
        $this->model::class,
    ));

`

should be changed to something like the following:

`
/**
* @return EloquentRelation
*/
private function getRelation(): EloquentRelation
{
$name = $this->relation->relationName();

    assert(method_exists($this->model, $name) || $this->model->relationResolver($this->model::class, $name), sprintf(
        'Expecting method %s to exist on model %s',
        $name,
        $this->model::class,
    ));

`

This way it will also work for relations declared with resolveRelationUsing

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0