# Basics
To search, you must specify which model fields will participate in the search.
To do this, you need to list them in the returned array in the search()
method.
If the method returns an empty array, then the search string will not be displayed.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Fields\Text;use MoonShine\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... public function search(): array { return ['id', 'title', 'text']; } //...}
# Full text search
If a fulltext search is required, then you must use the MoonShine\Attributes\SearchUsingFullText
attribute.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Attributes\SearchUsingFullText; use MoonShine\Fields\Text;use MoonShine\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... #[SearchUsingFullText(['title', 'text'])] public function search(): array { return ['id']; } //...}
Don't forget to add fulltext index
# Search by json keys
For Json fields that are used as a key-value keyValue()
,
you can specify which field key is involved in the search.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Fields\Text;use MoonShine\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... public function search(): array { return ['data->title']; } //...}
For multidimensional Json, which are formed through fields fields()
,
The search key must be specified as follows:
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Fields\Text;use MoonShine\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... public function search(): array { return ['data->[*]->title']; } //...}
# Search by relationship
You can search by relationships; to do this, you need to specify which the relationship field to search.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Fields\Text;use MoonShine\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... public function search(): array { return ['category.title']; } //...}
# Global search
In the MoonShine admin panel, global search can be implemented based on integration Laravel Scout .
To implement a global search you must:
1 Specify the list of models to search in the configuration fileconfig/moonshine.php
.
'global_search' => [ Article::class, User::class],
use MoonShine\Scout\HasGlobalSearch;use MoonShine\Scout\SearchableResponse;use Laravel\Scout\Searchable;use Laravel\Scout\Builder; class Article extends Model implements HasGlobalSearch{ use Searchable; public function searchableQuery(Builder $builder): Builder { return $builder->take(4); } public function toSearchableResponse(): SearchableResponse { return new SearchableResponse( group: 'Articles', title: $this->title, url: '/', preview: $this->text, image: $this->thumbnail ); }}