src | ||
tests | ||
composer.json | ||
CONTRIBUTING.md | ||
LICENSE | ||
README.md |
glmdev/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.
Install
Using Composer:
$ composer require glmdev/search
Update Laravel's providers array (app/config.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:
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:
BlogPost::search('search query');
Credits
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/>.