ModelResource

Filters

Filters are also created using fields: they 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 your model resource.

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;
 
use App\Models\Post;
use MoonShine\UI\Fields\Text;
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
//...
 
protected function filters(): iterable
{
return [
Text::make('Title', 'title'),
];
}
 
//...
}

filters filters_dark

Fields are a key element in building forms in the Moonshine admin panel. Learn more about fields

If you need to cache the state of the filters, use the saveQueryState property in the resource.

namespace App\MoonShine\Resources;
 
use App\Models\Post;
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
protected bool $saveQueryState = true;
//...
}