directus

Filter items either or in directus


I'm playing around with Directus and wanted to filter my items. I would like to receive all messages that either I am as sender or I am as receiver.

my get / messages looks like this

[
    {
        "content": "Hey!",
        "id": "1",
        "receiver": "1",
        "sender": "2"
    },
    {
        "content": "hello",
        "id": "2",
        "receiver": "2",
        "sender": "1"
    }
]

My backend request url looks like this

/items/messages?filter[sender]=${user_id}&filter[receiver]=${user_id}

But it is not working :( so please help me to get it done!


Solution

  • Try this:

    /items/messages?filter={"_or":[{"sender":{"_eq": "id"}},{"receiver":{"_eq":"id"}}]}
    

    Expanded version:

    "_or": [
        {
            "sender": {
                "_eq": "id"
            }
        },
        {
            "reveiver": {
                "_eq": "id"
            }
        }
    ]
    

    Visit the official docs to read more about filtering rules and logical operators.