Skip to content

[Laravel] Filtering by dot-notated relation fields in JSON:API does not work #7286

Open
@cay89

Description

@cay89

API Platform version(s) affected: 4.1.18

Description

I believe that filtering by relation fields using dot notation (as per the JSON:API specification) should work out of the box. However, it seems like this functionality is either not implemented or not working correctly, as attempting such a filter results in an error.

#[
    ApiResource(
        uriTemplate: '/posts',
        operations: [
            new GetCollection(
                parameters: [
                    new QueryParameter(
                        key: 'filter[author.name]',
                        filter: PartialSearchFilter::class,
                        property: 'author.name',
                    ),
                ],
            ),
        ],
    ),
]
class Post extends Model
{
    public function author()
    {
        return $this->belongsTo(User::class);
    }
}

class User extends Model
{
    // ...
}

Assuming this relationship is correctly exposed in the API, I would expect the following filter to work:

/api/posts?filter[author.name]=John

But instead, this request results an error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'author.name' in 'where clause' (Connection: mariadb, SQL: select count(*) as aggregate from posts where author.name like %John%)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions