fynduck / laravel-searchable
A description for laravel-searchable.
Installs: 583
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=7.0
Requires (Dev)
- orchestra/testbench: ^3.6
README
Install
composer require fynduck/laravel-searchable
Usage
use Fynduck\LaravelSearchable\src\Searchable; class User extends \Eloquent { use Searchable; /** * Searchable rules. * Columns and their priority in search results. * Columns with higher values are more important. * Columns with equal values have equal importance. * @var array * @return array */ protected function toSearchableArray() { return [ 'columns' => [ 'name' => 10, 'email' => 5, ], 'joins' => [ 'posts' => ['users.id','posts.user_id'], ], ]; } /** * Select fields * @return array */ public function selectFields() { return [ 'users.name', 'users.email' ]; } public function posts() { return $this->hasMany('Post'); } }
Now you can search your model.
// Simple search $users = User::search($query)->get(); // Search and get relations // It will not get the relations if you don't do this $users = User::search($query) ->with('posts') ->get();
Search Paginated
As easy as laravel default queries
// Search with relations and paginate $users = User::search($query) ->with('posts') ->paginate(20);
Mix queries
Search method is compatible with any eloquent method. You can do things like this:
// Search only active users $users = User::where('status', 'active') ->search($query) ->paginate(20);
Custom Threshold
The default threshold for accepted relevance is the sum of all attribute relevance divided by 4. To change this value you can pass in a second parameter to search() like so:
// Search with lower relevance threshold $users = User::where('status', 'active') ->search($query, 0) ->paginate(20);
The above, will return all users in order of relevance.
Entire Text search
By default, multi-word search terms are split and Searchable searches for each word individually. Relevance plays a role in prioritizing matches that matched on multiple words. If you want to prioritize matches that include the multi-word search (thus, without splitting into words) you can enable full text search by setting the third value to true. Example:
// Prioritize matches containing "John Doe" above matches containing only "John" or "Doe". $users = User::search("John Doe", null, true)->get();
If you explicitly want to search for full text matches only, you can disable multi-word splitting by setting the fourth parameter to true.
// Do not include matches that only matched "John" OR "Doe". $users = User::search("John Doe", null, true, true)->get();
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email DummyAuthorEmail instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.