add README
This commit is contained in:
parent
2c34bfb538
commit
adf9e86584
69
README.md
Normal file
69
README.md
Normal file
@ -0,0 +1,69 @@
|
||||
# element/search
|
||||
This is a simple hit based search engine for Eloquent models. It can take a string and will search the given set of models for each word in that string.
|
||||
It prioritizes models based on the number of times each word in the search string is found in the searchable fields in the model, with more emphasis
|
||||
on longer words, and less on shorter.
|
||||
|
||||
## Installation
|
||||
Element Search is available on composer. To install, simply run:
|
||||
``` bash
|
||||
composer require element/search
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
Making your models searchable is easy. The models simply need to implement the ```php Element\Search\SearchableContract ```
|
||||
interface. Then, add the `getSearchable()` method to the model. This method should return an array of the names
|
||||
of the Model's fields that the search engine can search. These fields should hold string/text data only.
|
||||
|
||||
For Example:
|
||||
|
||||
``` php
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Element\Search\SearchableContract;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BlogPost extends Model implements SearchableContract {
|
||||
protected $fillable = [ 'date-created', 'date-published', 'title', 'body',
|
||||
'author', 'image', 'viewcount' ];
|
||||
|
||||
public static function getSearchable(){
|
||||
return [ 'title', 'body', 'author' ];
|
||||
}
|
||||
|
||||
public static function search( $string ){
|
||||
return Search::search( new self, $string );
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
In the model above, we can see from the `getSearchable()` function that the Search Engine will search
|
||||
the 'title', 'body', and 'author' fields.
|
||||
|
||||
The second function is optional, but it allows you to call the search function statically from the
|
||||
model, rather than the search facade.
|
||||
|
||||
ie:
|
||||
``` php
|
||||
App\BlogPost::search('search string');
|
||||
```
|
||||
|
||||
## The Search Facade
|
||||
|
||||
The Element Search plugin adds a Search facade to the Laravel app. This allows you to call a
|
||||
search on any model from anywhere using the following function:
|
||||
|
||||
``` php
|
||||
Search::search( new App\ModelName, 'search string');
|
||||
```
|
||||
|
||||
Where `App\ModelName` is a new instance of the model you wish to search, and `'search string'` is
|
||||
any string you wish to search for. Additionally, you don't have to worry about removing punctuation
|
||||
or capital letters from your search string, the function does it automatically.
|
||||
|
||||
The search function, whether in the model, or called from the facade, returns the models in the form
|
||||
of a \Laravel\Database\Eloquent\Collection collection. This allows you to perform all the usual
|
||||
functions you would be able to perform on a database query.
|
Loading…
Reference in New Issue
Block a user