ModelResource

Search

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.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... protected function search(): array { return ['id', 'title', 'text']; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
protected function search(): array
{
return ['id', 'title', 'text'];
}
}

MoonShine Search Dark

Full-Text Search

If full-text search is required, you need to use the attribute MoonShine\Support\Attributes\SearchUsingFullText.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:4] namespace App\MoonShine\Resources; use MoonShine\Support\Attributes\SearchUsingFullText; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... #[SearchUsingFullText(['title', 'text'])] protected function search(): array { return ['id']; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Support\Attributes\SearchUsingFullText;
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
#[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.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... protected function search(): array { return ['data->title']; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
protected function search(): array
{
return ['data->title'];
}
}

For multidimensional Json, which are formed through fields(), the search key should be specified as follows:

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... protected function search(): array { return ['data->[*]->title']; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
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.

// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:3] namespace App\MoonShine\Resources; use MoonShine\Laravel\Resources\ModelResource; class PostResource extends ModelResource { // ... protected function search(): array { return ['category.title']; } }
 namespaces
namespace App\MoonShine\Resources;
 
use MoonShine\Laravel\Resources\ModelResource;
 
class PostResource extends ModelResource
{
// ...
 
protected function search(): array
{
return ['category.title'];
}
}

Global Search

In MoonShine, you can implement global search based on integration with Laravel Scout.

To implement global search, you need to:

  1. Install the moonshine/scout package:
composer require moonshine/scout
composer require moonshine/scout
php artisan vendor:publish --provider="MoonShine\Scout\Providers\ScoutServiceProvider"
php artisan vendor:publish --provider="MoonShine\Scout\Providers\ScoutServiceProvider"
  1. Specify the list of models for searching in the configuration file config/moonshine-scout.php.
'models' => [ Article::class, User::class ],
'models' => [
Article::class,
User::class
],
  1. Implement the interface in the models.
// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:6] namespace App\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 ); } }
 namespaces
namespace App\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
);
}
}
  1. Replace the component in Layout.
// torchlight! {"summaryCollapsedIndicator": "namespaces"} // [tl! collapse:4] use MoonShine\Laravel\Components\Layout\Locales; use MoonShine\Laravel\Components\Layout\Notifications; use MoonShine\UI\Components\Layout\Header; use MoonShine\UI\Components\Breadcrumbs; 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(), ]); }
 namespaces
use MoonShine\Laravel\Components\Layout\Locales;
use MoonShine\Laravel\Components\Layout\Notifications;
use MoonShine\UI\Components\Layout\Header;
use MoonShine\UI\Components\Breadcrumbs;
 
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(),
]);
}