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

How to filter by morph relation? #309

Open
MyLittleVerdant opened this issue Mar 20, 2025 · 0 comments
Open

How to filter by morph relation? #309

MyLittleVerdant opened this issue Mar 20, 2025 · 0 comments

Comments

@MyLittleVerdant
Copy link

For an illustrative example. I have model AgreementPartner with fields entity_type, entity_id. I described morph relation with other models like this

In Models/AgreementPartner:

 public function partnerable(): MorphTo
    {
        return $this->morphTo(__FUNCTION__, 'entity_type', 'entity_id');
    }

In AgreementPartnerSchema i have:

 public function fields(): iterable
    {
        return [
            ID::make()->uuid(),
        
            Str::make('entityType'),
            Str::make('entityId'),

            MorphTo::make('partnerable')->types(CarrierSchema::type(), MerchantSchema::type()),
        ];
    }

  public function filters(): iterable
    {
        return [
            WhereIdIn::make($this)->delimiter(','),

            WhereHas::make($this, 'partnerable'),
        ];
    }

And in AgreementPartnerResource:

class AgreementPartnerResource extends JsonApiResource
{
    public function relationships($request): iterable
    {
        return [
            $this->relation('agreements'),
            $this->relation('partnerable'),
        ];
    }
}

It's works fine for
/agreement-partners?include=partnerable
but i can't do this
/agreement-partners?filter[partnerable][id]=01958f45-74ff-72af-9d72-6b081b485f9a

Although if I change the description of the relation in fields() to this

BelongsTo::make('partnerable')->type(CarrierSchema::type()),

Everything will work and even add filters filter[partnerable][] for all related resources, despite the fact that now only CarrierSchema is described.

But it's seems like bug for me. Is there any other option for filtering by morph relation?

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

No branches or pull requests

1 participant