vits/laravel-simple-search
最新稳定版本:0.1
Composer 安装命令:
composer require vits/laravel-simple-search
包简介
Simple search scope for Laravel models.
README 文档
README
Search for words in one or more text fields (string or text) using provided search strategies.
Search query string is split into words and each of these words must match at least one of given fields. Each field may have different search strategy.
NB: Some of search strategies may work only with MySQL database driver.
Installation
composer require vits/laravel-simple-search
Usage
SimpleSearch model trait adds simpleSearch scope. Scope accepts search query string and optional list of fields to be searched. Searched fields and default search strategy may also be configured as model properties.
use Vits\LaravelSimpleSearch\SimpleSearch; use Vits\LaravelSimpleSearch\SimpleSearchStrategy; class Book extends Model { use SimpleSearch; protected $simpleSearchStrategy = SimpleSearchStrategy::IN_WORDS; protected $simpleSearchFields = 'title,body'; }
$books = Book::simpleSearch('alice', 'title,body')->get();
Available search strategies
SimpleSearch::EXACT - field value must be exact match of search string.
SimpleSearch::IN_WORDS - field must contain search string anywhere in it.
SimpleSearch::START_OF_WORDS - field must contain word starting with search string.
This ir default strategy.
SimpleSearch::START_OF_STRING - field value must start with search string.
Different ways to define searched fields
This may be used both in simpleSearch scope and as model property.
If field definition does not assign search strategy, value of model property simpleSearchStategy is used as default strategy. If not set, SimpleSearch::START_OF_WORDS is default search strategy.
# single field protected $simpleSearchFields = 'title'; # multiple fields protected $simpleSearchFields = 'alice', 'title,description'; # multiple fields as array protected $simpleSearchFields = 'alice', ['title', 'description']; # multiple fields as array with strategy protected $simpleSearchFields = 'alice', [ 'title', 'description', 'notes' => SimpleSearchStrategy::IN_WORD ];
统计信息
- 总下载量: 34
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-22