Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Runway support filtering and sorting for Eloquent models
    Statamic Runway Eloquent models REST API filters GraphQL API sorting filtering Statamic addons


How does Runway support filtering and sorting for Eloquent models


Runway provides several ways to support filtering and sorting for Eloquent models in Statamic:

1. Allowed Filters in the REST API:
- You can enable filtering for your Runway resources in the `config/statamic/api.php` configuration file by defining a list of `allowed_filters` for each resource.
- For example:

php
     'runway' => [
         'products' => [
             'allowed_filters' => ['name', 'slug'],
         ],
     ],
     

- This allows you to filter the REST API responses for the `products` resource by `name` and `slug` fields.

2. Eloquent Scopes in the GraphQL API:
- Runway extends Statamic's built-in GraphQL API to allow querying your Eloquent models.
- You can use Eloquent scopes in your GraphQL queries to filter and sort the results.
- For example, in your GraphQL query:

graphql
     products(limit: 25, sort: "name") {
         data {
             id
             name
             price
             description
         }
     }
     

- This will fetch the first 25 products sorted by the `name` field.

3. Filtering and Sorting in the Runway Tag:
- The `< runway:product >` tag supports filtering and sorting using Eloquent scopes.
- You can pass parameters to the tag to filter and sort the results.
- For example:

html
     {{ runway:product sort="name" filter:price="100,150" }}
         <h2>{{ name }}</h2>
         <p>Price: {{ price }}</p>
     {{ /runway:product }}
     

- This will display products with a price between 100 and 150, sorted by the `name` field.

By leveraging these features, you can easily filter and sort your Eloquent models in Statamic using Runway, whether you're using the REST API, GraphQL API, or the Runway tag in your templates.

Citations:
[1] https://github.com/mehdi-fathi/eloquent-filter
[2] https://statamic.com/addons/rad-pack/runway
[3] https://statamic.com/addons/rad-pack/runway/release-notes
[4] https://cosme.dev/post/filter-eloquent-models-with-multiple-optional-filters
[5] https://runway.duncanmcclean.com/rest-api