Для поиска необходимо указать, какие поля модели будут участвовать в поиске. Для этого нужно перечислить их в возвращаемом массиве в методе 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 ['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'];
}
//...
}
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'];
}
//...
}

Если требуется полнотекстовый поиск, то необходимо использовать атрибут MoonShine\Attributes\SearchUsingFullText
.
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'];
}
}
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'];
}
//...
}
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'];
}
//...
}
Не забудьте добавить полнотекстовый индекс
Для Json полей, которые используются в качестве ключ-значение keyValue()
, можно указать, какой ключ поля участвует в поиске.
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'];
}
}
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'];
}
//...
}
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'];
}
//...
}
Для многомерных Json, которые формируются через поля fields()
, ключ поиска нужно указывать следующим образом:
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'];
}
}
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'];
}
//...
}
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'];
}
//...
}
Вы можете осуществлять поиск по связям; для этого нужно указать, по какому полю связи осуществлять поиск.
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'];
}
}
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'];
}
//...
}
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'];
}
//...
}
В админ-панели MoonShine можно реализовать глобальный поиск на основе интеграции
Laravel Scout.
Для реализации глобального поиска необходимо:
- Указать список моделей для поиска в конфигурационном файле
config/moonshine.php
.
'global_search' => [
Article::class,
User::class
],
'global_search' => [
Article::class,
User::class
],
'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
);
}
}
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
);
}
}