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

filtering on pivot seems to not work properly #143

Open
FXLaroche opened this issue Nov 14, 2024 · 1 comment
Open

filtering on pivot seems to not work properly #143

FXLaroche opened this issue Nov 14, 2024 · 1 comment

Comments

@FXLaroche
Copy link

Laravel Rest Api Version

2.8.7

Laravel Version

11

PHP Version

8.3

Database Driver & Version

No response

Description

Considering the following example:

I want to search a team, and join to the result the users who are part of this team, and amongst these users, those who have the value 'left_at' at NULL on the pivot table. Here is my payload:

{
    "search": {
        "filters": [
            {
                "field": "id",
                "operator": "=",
                "value": "9d6ceb1a-3573-4ad7-837b-424637fa4905"
            },
            {
                "field": "users.pivot.left_at",
                "operator": "=",
                "value": null
            }
        ],
        "includes": [
            {
                "relation": "users",
                "filters": 
                [
                    {
                        "field": "teams.pivot.left_at",
                        "operator": "=",
                        "value": null
                    }
                ]
            }
        ],
        "sorts": [
            {
                "field": "label",
                "direction": "asc"
            }
        ],
        "page": 1,
        "limit": 10
    }
}

For my tests I've filtered on the object and its relation independently, as well as on both.

However in any case I get results where 'left_at' is not null.

{
    "current_page": 1,
    "data": [
        {
            "id": "9d6ceb1a-3573-4ad7-837b-424637fa4905",
            "label": "Team1 ",
            "created_at": "2024-11-06T16:41:03.000000Z",
            "updated_at": "2024-11-06T16:41:03.000000Z",
            "users": [
                {
                    "id": "9d6ceb1a-3da8-4e72-ad49-f3d81bbd63c1",
                    "firstname": "Arthur",
                    "lastname": "Mercier",
                    "email": "[email protected]",
                    "pivot": {
                        "left_at": "2024-10-15",
                    },
                },
                {
                    "id": "9d6ceb1a-4958-4efd-884a-43ae870596cc",
                    "firstname": "Matthieu",
                    "lastname": "Rousset",
                    "email": "[email protected]",
                    "pivot": {
                        "left_at": null,
                    },
                },
                {
                {
                    "id": "9d6ceb1a-4f0f-44a9-8392-421642d0a794",
                    "site_id": "9d6ceb16-e4c6-41fc-a4a4-6230e49467ec",
                    "firstname": "Roger",
                    "lastname": "Potier",
                    "email": "[email protected]",
                    "pivot": {
                        "left_at": null,
                    },
                },
                {
                    "id": "9d6ceb1a-435a-47e6-97a3-9c3b0897b354",
                    "site_id": "9d6ceb16-e4c6-41fc-a4a4-6230e49467ec",
                    "firstname": "Yves",
                    "lastname": "Marion",
                    "email": "[email protected]",
                    "pivot": {
                        "left_at": "2024-05-08",
                    },
                }
            ]
        }
    ],
    "from": 1,
    "last_page": 1,
    "per_page": 10,
    "to": 1,
    "total": 1,
    "meta": []
}

It seems that the filter on the pivot table is not working at all, or that I misunderstood the way it works.
I've found a way around using a scope, but it would be nice to have it filter properly.

Steps To Reproduce

Make a search request on a model with a many-to-many relationship, inluding the relationship and filtering on a data on the pivot table.

@GautierDele
Copy link
Member

This could be related to include, feels strange to me
I'll cover this with an unit test and come back to you depending on the result

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

2 participants