该软件包可以轻松地从各种来源获得结构化搜索。这是我们搜索一些模型的示例。我们已经对模型本身做了一些小的准备。
$ searchResults = ( new Search ())
-> registerModel ( User ::class, ' name ' )
-> registerModel ( BlogPost ::class, ' title ' )
-> search ( ' john ' );
搜索将不区分大小写。 $searchResults
现在包含name
属性中包含john
所有User
模型以及title
属性中包含 'john' 的BlogPost
。
在您看来,您现在可以循环搜索结果:
< 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
在此示例中,我们使用了模型,但您可以轻松添加外部 API、文件列表或值数组的搜索方面。
我们投入了大量资源来创建一流的开源包。您可以通过购买我们的一款付费产品来支持我们。
我们非常感谢您从家乡寄给我们一张明信片,并注明您正在使用我们的哪种套餐。您可以在我们的联系页面上找到我们的地址。我们在虚拟明信片墙上发布所有收到的明信片。
您可以通过 Composer 安装该软件包:
composer require spatie/laravel-searchable
为了搜索模型,您必须让它们实现Searchable
接口。
namespace Spatie Searchable ;
interface Searchable
{
public function getSearchResult (): SearchResult ;
}
您只需向每个必须返回SearchResult
实例的可搜索模型添加一个getSearchResult
方法。这是博客文章模型的样子。
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
);
}
}
准备好模型后,您可以像这样搜索它们:
$ searchResults = ( new Search ())
-> registerModel ( User ::class, ' name ' )
-> search ( ' john ' );
搜索将不区分大小写。 $searchResults
现在包含name
属性中包含john
的所有User
模型。
您还可以传递多个属性进行搜索:
// 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 ' );
要获得细粒度的控制,您还可以使用可调用的。通过这种方式,您还可以搜索精确匹配、应用范围、急切加载关系,甚至像使用查询生成器一样过滤查询。
$ 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 ' );
});
您不仅限于将基本模型注册为搜索方面。您可以通过扩展SearchAspect
类轻松创建自己的自定义搜索方面。
请考虑以下自定义搜索方面来搜索外部 API:
class OrderSearchAspect extends SearchAspect
{
public function getResults ( string $ term ): Collection
{
return OrderApi :: searchOrders ( $ term );
}
}
您可以这样使用它:
$ searchResults = ( new Search ())
-> registerAspect ( OrderSearchAspect ::class)
-> search ( ' john ' );
可以通过在执行搜索之前调用limitAspectResults
来限制每个方面返回的结果数量。
$ searchResults = ( new Search ())
-> registerAspect ( BlogPostAspect ::class)
-> limitAspectResults ( 50 )
-> search ( ' How To ' );
以下是呈现搜索结果的示例:
< 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
您可以通过在模型或自定义搜索方面添加公共属性$searchableType
来自定义$type
class BlogPost extends Model implements Searchable
{
public $ searchableType = ' custom named aspect ' ;
}
composer test
请参阅变更日志以了解有关最近更改内容的更多信息。
详细信息请参阅贡献。
如果您发现有关安全的错误,请发送邮件至 [email protected],而不是使用问题跟踪器。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。