Download the PHP package james2doyle/laravel-scout-sonic without Composer
On this page you can find all versions of the php package james2doyle/laravel-scout-sonic. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download james2doyle/laravel-scout-sonic
More information about james2doyle/laravel-scout-sonic
Files in james2doyle/laravel-scout-sonic
Package laravel-scout-sonic
Short Description Sonic driver for Laravel Scout.
License MIT
Informations about the package laravel-scout-sonic
Laravel Scout Sonic Driver
Search Eloquent Models using Sonic indexes.
- Implementation
- Installation
- Usage
Implementation
When implementing the toSearchableArray
method, you need to provide an array that will be coerced into a string (the engine just joins with a ' '
) as Sonic can only index strings. So you need to provide a "stringified" (index string) version of your model. The default toArray
works but it is probably too much noise for reasonable usage.
Here is an example of the string I used when I was developing this Engine:
For me, this builds a nice string for search that can match on a "name". In my application, the concept of "name" is either the User display name or first/last name.
If the locale is known, you can also create the getSonicLocale()
method on your model, which returns the locale. It will then get passed to the Sonic PUSH
calls:
Installation
If you haven't already you should install Laravel Scout to
your project and apply the Laravel\Scout\Searchable
trait to any Eloquent models you would like to make searchable.
Install this package via Composer
composer require james2doyle/laravel-scout-sonic
Note: if you have Laravel >= 5.5 you can skip this step because of Package Auto-Discovery.
Next add the ServiceProvider to the Package Service Providers in config/app.php
Append the default configuration to config/scout.php
Set SCOUT_DRIVER=sonic
in your .env
file
In addition there is no need to use the php artisan scout:import
command.
Usage
Simply call the search()
method on your Searchable
models:
$users = App\User::search('bro')->get();
Simple constraints can be applied using the where()
builder method:
$users = App\User::search('bro')->where('active', 1)->get();
Note: Sonic does not support the concept of "where", so the where is applied at the collection level not the query!
Pagination
Sonic cannot support real pagination because Sonic does not return proper paging or total information. It simply returns all the results for a given query.
There is a naive implementation of pagination in place but it probably isn't perfect as it doesn't take into account the "where" filter.
For more usage information see the Laravel Scout Documentation.