ModelResource

Quick Filters (Tags)

Basics

Sometimes there is a need to create filters (result selection) and display them in the listing. Tags were created for such situations.

 namespaces
namespace App\MoonShine\Resources\Post\Pages;
 
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
use MoonShine\Laravel\Pages\Crud\IndexPage;
 
class PostIndexPage extends IndexPage
{
// ...
 
protected function queryTags(): array
{
return [
QueryTag::make(
'Post with author', // Tag title
fn(Builder $query) => $query->whereNotNull('author_id') // Query builder
)
];
}
}
 namespaces
namespace App\MoonShine\Resources\Post\Pages;
 
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
use MoonShine\Laravel\Pages\Crud\IndexPage;
 
class PostIndexPage extends IndexPage
{
// ...
 
protected function queryTags(): array
{
return [
QueryTag::make(
'Post with author', // Tag title
fn(Builder $query) => $query->whereNotNull('author_id') // Query builder
)
];
}
}

query_tags query_tags_dark

The queryTags() method is also available in the ModelResource class for backward compatibility, but it is recommended to define tags directly in IndexPage.

Icon

You can add an icon to the tag using the icon() method.

 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'Post without an author',
fn(Builder $query) => $query->whereNull('author_id')
)
->icon('users')
 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'Post without an author',
fn(Builder $query) => $query->whereNull('author_id')
)
->icon('users')

For more detailed information, please refer to the section Icons.

Active Item

You can make a QueryTag active by default. To do this, use the default() method.

default(Closure|bool|null $condition = null)
default(Closure|bool|null $condition = null)
 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'All posts',
fn(Builder $query) => $query
)
->default()
 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'All posts',
fn(Builder $query) => $query
)
->default()

Display Condition

You might want to display tags only under certain conditions. You can use the canSee() method, which requires you to pass a callback function that returns TRUE or FALSE.

 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'Post with author', // Tag title
fn(Builder $query) => $query->whereNotNull('author_id')
)
->canSee(fn() => auth()->user()->moonshine_user_role_id === 1)
 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'Post with author', // Tag title
fn(Builder $query) => $query->whereNotNull('author_id')
)
->canSee(fn() => auth()->user()->moonshine_user_role_id === 1)

Alias

By default, the URL value is automatically generated from the label parameter. All non-Latin alphabet characters are replaced with their corresponding transliteration, e.g., 'Заголовок' => 'zagolovok'.

The alias() method allows you to set a custom value for the URL.

 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'Archived Posts',
fn(Builder $query) => $query->where('is_archived', true)
)
->alias('archive')
 namespaces
use Illuminate\Database\Eloquent\Builder;
use MoonShine\Laravel\QueryTags\QueryTag;
 
QueryTag::make(
'Archived Posts',
fn(Builder $query) => $query->where('is_archived', true)
)
->alias('archive')

By default, all tag buttons are displayed in a line, but you can display them through a drop-down list. To do this, change the $queryTagsInDropdown property in the resource.

class PostResource extends ModelResource
{
protected bool $queryTagsInDropdown = true;
 
// ...
}
class PostResource extends ModelResource
{
protected bool $queryTagsInDropdown = true;
 
// ...
}

Events

Raising custom events after a successful data update:

QueryTag::make('QueryTag', fn($q) => $q)->events([
AlpineJs::event(
JsEvent::FRAGMENT_UPDATED, 'custom-fragment'
)
])
QueryTag::make('QueryTag', fn($q) => $q)->events([
AlpineJs::event(
JsEvent::FRAGMENT_UPDATED, 'custom-fragment'
)
])

Button modifier

QueryTag::make('QueryTag', fn($q) => $q)
->modifyButton(fn(ActionButton $btn) => $btn)
QueryTag::make('QueryTag', fn($q) => $q)
->modifyButton(fn(ActionButton $btn) => $btn)