Basics
Sometimes there is a need to create filters (result selection) and display them in the listing. Tags were created for such situations.
namespace App\MoonShine\Resources; use App\Models\Post;use Illuminate\Database\Eloquent\Builder;use MoonShine\Laravel\QueryTags\QueryTag;use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... protected function queryTags(): array { return [ QueryTag::make( 'Post with author', // Tag title fn(Builder $query) => $query->whereNotNull('author_id') // Query builder ) ]; } //...}
Icon
You can add an icon to the tag using the icon()
method.
use Illuminate\Database\Eloquent\Builder;//... 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)
use Illuminate\Database\Eloquent\Builder; //... 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
.
use Illuminate\Database\Eloquent\Builder; //... 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.
use Illuminate\Database\Eloquent\Builder; //... QueryTag::make( 'Archived Posts', fn(Builder $query) => $query->where('is_archived', true)) ->alias('archive')