Basics
To perform a search, you need to specify which fields of the model will participate in the search. For this, you need to list them in the returned array in the search()
method.
If the method returns an empty array, the search string will not be displayed.
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 search(): array { return ['id', 'title', 'text']; } //...}
Full-Text Search
If full-text search is required, you need to use the attribute MoonShine\Support\Attributes\SearchUsingFullText
.
namespace App\MoonShine\Resources; use App\Models\Post;use MoonShine\Support\Attributes\SearchUsingFullText;use MoonShine\UI\Fields\Text;use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... #[SearchUsingFullText(['title', 'text'])] protected function search(): array { return ['id']; } //...}
Don't forget to add a full-text index
JSON Key Search
For Json
fields that are used as key-value keyValue()
, you can specify which field key participates in the search.
class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... protected function search(): array { return ['data->title']; } //...}
For multidimensional Json
, which are formed through fields()
, the search key should be specified as follows:
class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... protected function search(): array { return ['data->[*]->title']; } //...}
Relation Search
You can perform a search on relations; for this, you need to specify which relation field to search by.
class PostResource extends ModelResource{ protected string $model = Post::class; protected string $title = 'Posts'; //... protected function search(): array { return ['category.title']; } //...}
Global Search
In the MoonShine admin panel, you can implement global search based on integration with Laravel Scout.
To implement global search, you need to:
- Install the
moonshine/scout
package:
composer require moonshine/scout
php artisan vendor:publish --provider="MoonShine\Scout\Providers\ScoutServiceProvider"
- Specify the list of models for searching in the configuration file
config/moonshine-scout.php
.
'models' => [ Article::class, User::class],
- Implement the interface in the models.
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 ); }}
- Replace the component in
Layout
protected function getHeaderComponent(): Header{ return Header::make([ Breadcrumbs::make($this->getPage()->getBreadcrumbs())->prepend($this->getHomeUrl(), icon: 'home'), \MoonShine\Scout\Components\Search::make(), When::make( fn (): bool => $this->isUseNotifications(), static fn (): array => [Notifications::make()] ), Locales::make(), ]);}