Laravel Global Search
View Package
Overview: Laravel Global Search is a flexible Laravel package designed to simplify global searches across multiple models within a Laravel application. With support for custom and default models, configurable search behavior, and robust error handling, developers can effortlessly integrate powerful search functionality tailored to their application's requirements.
Installation: Begin by installing the package via Composer:
composer require dipesh79/laravel-global-search
Next, publish the configuration file to customize settings:
php artisan vendor:publish --provider="Dipesh79\LaravelGlobalSearch\LaravelGlobalSearchServiceProvider" --tag="config"
Configuration: Customize default models, search operator, and allow empty query search behavior in the published config file.
Usage:
- Enable global search functionality in your models by using the Searchable trait:
<?php use Dipesh79\LaravelGlobalSearch\Traits\Searchable; class User extends Model { use Searchable; }
- Specify the columns to be searched on the model:
<?php use Dipesh79\LaravelGlobalSearch\Traits\Searchable; class User extends Model { use Searchable; protected array $searchable = ['name', 'email']; }
- Perform global searches using the searchGlobally method:
<?php use App\Models\User; $users = User::searchGlobally('John Doe');
- Optionally, search across multiple models by specifying them in the config file or passing them as parameters:
<?php use Dipesh79\LaravelGlobalSearch\LaravelGlobalSearch; $models = [ \App\Models\User::class, \App\Models\Post::class, ]; $results = LaravelGlobalSearch::searchGlobally('John Doe', $models, 'like');
Best Practices:
- Define custom models in the config file to search across specific models.
- Use the Searchable trait in your models to enable global search.
- Specify searchable columns using the $searchable property.
- Define the search operator in the config file or pass it as a parameter.
- Handle errors gracefully by checking for empty results or exceptions.
Contributing: Contributions, feedback, and pull requests are welcome! Help improve the Laravel Global Search package for the Laravel community.
License: The Laravel Global Search package is open-source software licensed under the MIT license.
Credits:
- Dipesh79
- All Contributors
Author: @Dipesh79
Support: For support, email dipeshkhanal79[at]gmail[dot]com.