該軟體包可以輕鬆地從各種來源獲得結構化搜尋。這是我們搜尋一些模型的範例。我們已經對模型本身做了一些小小的準備。
$ 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)。請參閱許可證文件以獲取更多資訊。