hamzaejaz / laravel-searchable-scope
A Laravel trait to add dynamic search to models
Installs: 1 730
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 1
Requires
- php: ^8.0
Requires (Dev)
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.0
README
A powerful and flexible search trait for Laravel Eloquent models that makes implementing search functionality a breeze.
Features
- Simple and intuitive API
- Configurable search behavior
- Support for related model searching
- Case-sensitive/insensitive search options
- Multiple search operators support
- Minimum search term length configuration
- Default search columns configuration
Installation
You can install the package via composer:
composer require hamzaejaz/laravel-searchable-scope
The package will automatically register its service provider.
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="HamzaEjaz\SearchableScope\SearchableScopeServiceProvider" --tag="config"
This will create a config/searchable-scope.php
file where you can modify the default settings.
Usage
Basic Usage
Add the trait to your model:
use HamzaEjaz\SearchableScope\Traits\Searchable; class User extends Model { use Searchable; }
Then you can search like this:
// Search in all default columns User::search('john')->get(); // Search in specific columns User::search('john', ['name', 'email'])->get(); // Search in related models User::search('john', [], ['posts' => ['title', 'content']])->get();
Advanced Usage
You can define default searchable columns and relations in your model:
class User extends Model { use Searchable; protected $searchable = [ 'columns' => ['name', 'email', 'username'], 'relations' => [ 'posts' => ['title', 'content'], 'profile' => ['bio', 'location'] ] ]; }
Now you can simply call:
User::search('john')->get();
Configuration Options
The package comes with several configuration options in config/searchable-scope.php
:
default_operator
: The default search operator (LIKE, =, >, <, >=, <=)case_sensitive
: Whether the search should be case sensitivedefault_columns
: Default columns to search if none specifiedmin_term_length
: Minimum length of search term before search is performed
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.