spatie/laravel-searchable
Composer 安装命令:
composer require spatie/laravel-searchable
包简介
Pragmatically search through models and other sources
README 文档
README
This package makes it easy to get structured search from a variety of sources. Here's an example where we search through some models. We already did some small preparation on the models themselves.
$searchResults = (new Search()) ->registerModel(User::class, 'name') ->registerModel(BlogPost::class, 'title') ->search('john');
The search will be performed case insensitive. $searchResults now contains all User models that contain john in the name attribute and BlogPosts that contain 'john' in the title attribute.
In your view you can now loop over the search results:
<h1>Search</h1> There are {{ $searchResults->count() }} results. @foreach($searchResults->groupByType() as $type => $modelSearchResults) <h2>{{ $type }}</h2> @foreach($modelSearchResults as $searchResult) <ul> <li><a href="{{ $searchResult->url }}">{{ $searchResult->title }}</a></li> </ul> @endforeach @endforeach
In this example we used models, but you can easily add a search aspect for an external API, list of files or an array of values.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-searchable
Usage
Preparing your models
In order to search through models you'll have to let them implement the Searchable interface.
namespace Spatie\Searchable; interface Searchable { public function getSearchResult(): SearchResult; }
You'll only need to add a getSearchResult method to each searchable model that must return an instance of SearchResult. Here's how it could look like for a blog post model.
use Spatie\Searchable\Searchable; use Spatie\Searchable\SearchResult; class BlogPost extends Model implements Searchable { public function getSearchResult(): SearchResult { $url = route('blogPost.show', $this->slug); return new \Spatie\Searchable\SearchResult( $this, $this->title, $url ); } }
Searching models
With the models prepared you can search them like this:
$searchResults = (new Search()) ->registerModel(User::class, 'name') ->search('john');
The search will be performed case insensitive. $searchResults now contains all User models that contain john in the name attribute.
You can also pass multiple attributes to search through:
// use multiple model attributes $searchResults = (new Search()) ->registerModel(User::class, 'first_name', 'last_name') ->search('john'); // or use an array of model attributes $searchResults = (new Search()) ->registerModel(User::class, ['first_name', 'last_name']) ->search('john');
To get fine grained control you can also use a callable. This way you can also search for exact matches, apply scopes, eager load relationships, or even filter your query like you would using the query builder.
$search = (new Search()) ->registerModel(User::class, function(ModelSearchAspect $modelSearchAspect) { $modelSearchAspect ->addSearchableAttribute('name') // return results for partial matches on usernames ->addExactSearchableAttribute('email') // only return results that exactly match the e-mail address ->active() ->has('posts') ->with('roles'); });
Creating custom search aspects
You are not limited to only registering basic models as search aspects. You can easily create your own, custom search aspects by extending the SearchAspect class.
Consider the following custom search aspect to search an external API:
class OrderSearchAspect extends SearchAspect { public function getResults(string $term): Collection { return OrderApi::searchOrders($term); } }
This is how you can use it:
$searchResults = (new Search()) ->registerAspect(OrderSearchAspect::class) ->search('john');
Limiting aspect results
It is possible to limit the amount of results returned by each aspect by calling limitAspectResults prior to performing the search.
$searchResults = (new Search()) ->registerAspect(BlogPostAspect::class) ->limitAspectResults(50) ->search('How To');
Rendering search results
Here's an example on rendering search results:
<h1>Search</h1> There are {{ $searchResults->count() }} results. @foreach($searchResults->groupByType() as $type => $modelSearchResults) <h2>{{ $type }}</h2> @foreach($modelSearchResults as $searchResult) <ul> <a href="{{ $searchResult->url }}">{{ $searchResult->title }}</a> </ul> @endforeach @endforeach
You can customize the $type by adding a public property $searchableType on your model or custom search aspect
class BlogPost extends Model implements Searchable { public $searchableType = 'custom named aspect'; }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/laravel-searchable 适用场景与选型建议
spatie/laravel-searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.58M 次下载、GitHub Stars 达 1.37k, 最近一次更新时间为 2018 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-searchable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-searchable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-searchable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-searchable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easy table searching for Laravel
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
统计信息
- 总下载量: 1.58M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1379
- 点击次数: 13
- 依赖项目数: 29
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-07