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'];
}
//...
}
namespaceApp\MoonShine\Resources;
useApp\Models\Post;
useMoonShine\Fields\Text;
useMoonShine\Resources\ModelResource;
classPostResourceextendsModelResource
{
protectedstring $model =Post::class;
protectedstring $title ='Posts';
//...
publicfunctionsearch():array
{
return ['id', 'title', 'text'];
}
//...
}
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'];
}
//...
}
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
);
}
}
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
);
}
}