Search Plugin
Adds full-text searching capabilities to Winter, built on the foundations of Laravel Scout. The plugin acts primarily as a wrapper for Laravel Scout, and provides its entire suite of functionality within Winter's architecture, but also includes additional capabilities to make its use in Winter even easier.
Getting started
To install the plugin, you may install it through the Winter CMS Marketplace, or you may install it using Composer:
composer require winter/wn-search-plugin
Then, run the migrations to ensure the plugin is enabled:
php artisan winter:up
Configuration
Configuration for this plugin is chiefly done through the search.php
configuration file. You can publish this configuration into your project's config
directory by running the following command:
php artisan vendor:publish --provider="Winter\Search\Plugin"
This will create your own configuration file at config/winter/search/search.php
, in which you will be able to override all default configuration values.
Usage
As this is a wrapper, you can use all the base functionality that Laravel Scout provides. There are only a couple of subtle differences with the Search plugin's implementation:
- Configuration values are stored within the
search
key. Wherever there is mention of ascout
configuration value, you must usesearch
instead. - Soft deleted models are determined by the usage of the
Winter\Storm\Database\Traits\SoftDelete
trait, not the base LaravelSoftDeletes
trait.
To make a particular model searchable, you simply add the Winter\Search\Traits\Searchable
trait to that model. This trait will register a model observer that will automatically synchronise the model records to an index:
<?php
namespace Winter\Plugin\Models;
use Model;
use Winter\Search\Traits\Searchable;
class MyModel extends Model
{
use Searchable;
}
As the model is created, updated or deleted, the index will automatically be updated to reflect the state of that model record.
Configuring searchable data
By default, the entire model is converted to an array form and persisted in the search index. If you would like to limit the data that is stored in the index, you can provide a $searchable
property in the model. This property will represent all the model attributes that you would like to store in the index:
<?php
namespace Winter\Plugin\Models;
use Model;
use Winter\Search\Traits\Searchable;
class Post extends Model
{
use Searchable;
public $searchable = [
'title',
'summary'
];
}
If you want even more control over the data, you may override the toSearchableArray
method:
<?php
namespace Winter\Plugin\Models;
use Model;
use Winter\Search\Traits\Searchable;
class Post extends Model
{
use Searchable;
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
// Customize the data array...
return $array;
}
}
Configuring the model ID
Normally, the primary key of the model will act as the model's unique ID that is stored in the search index. If you wish to use another column to act as the identifier for a model, you may override the getSearchKey
and getSearchKeyName
methods to customise this behaviour.
<?php
namespace Winter\Plugin\Models;
use Model;
use Winter\Search\Traits\Searchable;
class User extends Model
{
use Searchable;
/**
* Get the value used to index the model.
*
* @return mixed
*/
public function getSearchKey()
{
return $this->email;
}
/**
* Get the key name used to index the model.
*
* @return mixed
*/
public function getSearchKeyName()
{
return 'email';
}
}