karabinse / eloquent-searchable
Easily search eloquent models and their relations
Fund package maintenance!
Karabin
Installs: 1 404
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
- spatie/laravel-query-builder: ^5.0|^6.0
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
README
Installation
You can install the package via composer:
composer require karabinse/eloquent-searchable
Usage
Add the trait and add columns that are searchable.
use Karabin\Searchable\Concerns\IsSearchable; class User extends Authenticatable { use HasFactory, HasRoles, IsSearchable, Notifiable; protected $searchable = [ 'name', 'email', 'posts.title' ];
The package also includes a custom TermSearchFitler to be used with Laravel Query Builder.
use Karabin\Searchable\Filters\TermSearchFilter; class UserController extends Controller { public function index(Request $request) { $users = QueryBuilder::for(User::class) ->allowedFilters([ AllowedFilter::custom('search', new TermSearchFilter), ]) ->paginate($request->query('limit', 10)); return Inertia::render('User/Index', [ 'users' => UserData::collect($users), ]); }
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.