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
namespaceApp\MoonShine\Resources;
useMoonShine\Laravel\Resources\ModelResource;
classPostResourceextendsModelResource
{
// ...
protectedfunctionsearch():array
{
return ['id', 'title', 'text'];
}
}
namespace App\MoonShine\Resources;
use MoonShine\Laravel\Resources\ModelResource;
class PostResource extends ModelResource
{
// ...
protected function search(): array
{
return ['id', 'title', 'text'];
}
}
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
],
'models' => [
Article::class,
User::class
],
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
);
}
}
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
);
}
}
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(),
]);
}