Search code examples
laravellaravel-nova

Laravel Nova API - Can you apply a filter to a requested resource or lens


Is it possible (or has anyone found a way) to apply a filter when getting a lens via the API, ie;

http://my-site.test/nova-api/my-model/lens/my-lens?filter=MyFilter

The ability to add multiple filters would be cool too if anyone knows how


Solution

  • I worked out how to do it, the filters are passed through after being base64 encoded in $request->filters, example;

    [
        {"class":"App\\Nova\\Filters\\MyFilter","value":1}
    ]
    

    Let's say this is your "filters" payload, you base64 encode it;

    Ww0KICAgICAgICB7ImNsYXNzIjoiQXBwXFxOb3ZhXFxGaWx0ZXJzXFxNeUZpbHRlciIsInZhbHVlIjoxfQ0KICAgIF0=

    Then the URL you would pass through would be;

    http://my-site.test/nova-api/my-model/lens/my-lens?filters=Ww0KICAgICAgICB7ImNsYXNzIjoiQXBwXFxOb3ZhXFxGaWx0ZXJzXFxNeUZpbHRlciIsInZhbHVlIjoxfQ0KICAgIF0=