Go to file
2016-12-30 23:11:29 +00:00
src completed functionality and docs update 2016-12-30 23:05:11 +00:00
tests updated to new standard 2016-07-14 19:50:56 -05:00
composer.json completed functionality and docs update 2016-12-30 23:05:11 +00:00
CONTRIBUTING.md updated to new standard 2016-07-14 19:50:56 -05:00
LICENSE completed functionality and docs update 2016-12-30 23:05:11 +00:00
README.md updated readme 2016-12-30 23:11:29 +00:00

glmdev/search

Latest Version on Packagist License: AGPL v3 Author

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/>.