Basics
Filters are also created using fields. Filters are displayed only on the main page of the section.
To specify the fields to filter the data by, simply return an array with the necessary fields in the filters() method of IndexPage.
If the method is absent or returns an empty array, the filters will not be displayed.
Some fields cannot participate in the construction of the filtering query, so they will be automatically excluded from the list of filters.
namespace App\MoonShine\Resources\Post\Pages;use MoonShine\UI\Fields\Text;use MoonShine\Laravel\Pages\Crud\IndexPage;class PostIndexPage extends IndexPage{// ...protected function filters(): iterable{return [Text::make('Title', 'title'),];}}
namespace App\MoonShine\Resources\Post\Pages;use MoonShine\UI\Fields\Text;use MoonShine\Laravel\Pages\Crud\IndexPage;class PostIndexPage extends IndexPage{// ...protected function filters(): iterable{return [Text::make('Title', 'title'),];}}

Fields are a key element in building forms in Moonshine. Learn more about fields.
The filters() method is also available in the ModelResource class for backward compatibility, but it is recommended to define filters directly in IndexPage.
Apply
To redefine the filtering logic, you can use onApply() field method. We also suggest considering the process of applying fields using the example of filtering.
Cache state
If you need to cache the state of the filters, use the $saveQueryState property in the resource.
namespace App\MoonShine\Resources;use MoonShine\Laravel\Resources\ModelResource;class PostResource extends ModelResource{protected bool $saveQueryState = true;// ...}
namespace App\MoonShine\Resources;use MoonShine\Laravel\Resources\ModelResource;class PostResource extends ModelResource{protected bool $saveQueryState = true;// ...}