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

7 years ago
# glmdev/search
7 years ago
[![Latest Version on Packagist](https://img.shields.io/packagist/v/element/search.svg?maxAge=2592000&style=flat-square)](https://packagist.org/packages/glmdev/search)
7 years ago
[![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.
8 years ago
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:
8 years ago
``` bash
$ composer require glmdev/search
8 years ago
```
Update Laravel's providers array (`app/config.php`):
8 years ago
``` php
'providers' => [
// ... other providers ...
Glmdev\Search\SearchServiceProvider::class,
]
8 years ago
```
8 years ago
## 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.
8 years ago
Add the fields the engine should search in the `$searchable` variable.
8 years ago
For example:
8 years ago
``` php
8 years ago
namespace App;
use Glmdev\Search\SearchableContract;
use Glmdev\Search\SearchableAgreement;
8 years ago
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' ];
8 years ago
}
```
In the model above, we can see from the `$searchable` variable that the search engine will search
8 years ago
the 'title', 'body', and 'author' fields.
### Using the search functionality
8 years ago
Once your model is set up, you can search it using the `search()` function in the model, like so:
8 years ago
``` php
BlogPost::search('search query');
8 years ago
```
## 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/>.
```