You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.8 KiB

# glmdev/search
[![Latest Version on Packagist](https://img.shields.io/packagist/v/element/search.svg?maxAge=2592000&style=flat-square)](https://packagist.org/packages/glmdev/search)
[![License: AGPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg?style=flat-square)](LICENSE.md)
[![Author](http://img.shields.io/badge/author-Garrett_Mills-blue.svg?style=flat-square)](https://glmdev.github.io/)
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.
## Install
Using Composer:
``` bash
$ composer require glmdev/search
```
Update Laravel's providers array (`app/config.php`):
``` php
'providers' => [
// ... other providers ...
Glmdev\Search\SearchServiceProvider::class,
]
```
## Use
Making your models searchable is easy. The models need to implement the `Glmdev\Search\SearchableContract`
interface, use the `Glmdev\Search\SearchableAgreement` trait, and have the searchable fields defined.
Add the fields the engine should search in the `$searchable` variable.
For example:
``` php
namespace App;
use Glmdev\Search\SearchableContract;
use Glmdev\Search\SearchableAgreement;
use Illuminate\Database\Eloquent\Model;
class BlogPost extends Model implements SearchableContract {
use SearchableAgreement;
protected $fillable = [ 'date-created', 'date-published', 'title', 'body', 'author' ];
protected $searchable = [ 'title', 'body', 'author' ];
}
```
In the model above, we can see from the `$searchable` variable that the search engine will search
the 'title', 'body', and 'author' fields.
### Using the search functionality
Once your model is set up, you can search it using the `search()` function in the model, like so:
``` php
BlogPost::search('search query');
```
## Credits
- [Garrett Mills](https://glmdev.github.io/)
- [All Contributors](CONTRIBUTORS)
## License
The search package is licensed under the GNU General Public
License, version 3.0.
```
glmdev/search
Copyright (C) 2016 Garrett Mills
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
```