Skip to content

WhereHas filter issue #139

@X-Coder264

Description

@X-Coder264

I'm having some issues with the WhereHas filter.

This is the relation in my foo resource schema

BelongsToMany::make('domains')

and this is how the filter is defined

WhereHas::make($this, 'domains')

My domain resource schema has the ID filter defined -> Where::make('id')

Doing a request to my foo resource index endpoint with ?filter[domains][id]=1
throws Encountered filters that are not defined on the foo schema or domains relation: id exception (this happens in \LaravelJsonApi\Eloquent\QueryBuilder\Applicators\FilterApplicator::rejectUnrecognised)

The issue is that \LaravelJsonApi\Eloquent\QueryBuilder\Applicators\FilterApplicator::cursor does not return the relation filters at all because it calls \LaravelJsonApi\Eloquent\Fields\Relations\BelongsToMany::filters which does not yield any filters (because as far as I can see the only way to add filters is to use the withFilters method but I don't see any call to that method in the entire codebase of this package).

My current workaround is to define the relationship field as
BelongsToMany::make('domains')->withFilters(new Where('id')) // I'm manually calling withFilters here to make it work

which according to the documentation (https://laraveljsonapi.io/docs/1.0/schemas/filters.html#wherehas) shouldn't be necessary to do as it says

So in this example, the client can provide any filters that are valid for the comments resource

If I replace the \LaravelJsonApi\Eloquent\Filters\WhereHas::apply method logic from the current one to the one that was initially PR-ed in laravel-json-api/eloquent#16 then it works without having to manually call the withFilters method.

The issue is maybe related to #121

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions