microweber-packages / microweber-searchable
A framework-agnostic Laravel trait that adds searchable field definitions to Eloquent models. Works on all SQL engines.
Package info
github.com/microweber-packages/microweber-searchable
pkg:composer/microweber-packages/microweber-searchable
v1.0.0
2026-11-03 11:18 UTC
Requires
- php: ^8.2
- illuminate/database: ^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- microweber-packages/microweber-package: *
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
- microweber-packages/searchable: *
This package is auto-updated.
Last update: 2026-11-03 17:07:33 UTC
README
A standalone Laravel package that adds searchable field definitions to Eloquent models. Works on all SQL engines (MySQL, PostgreSQL, SQLite, SQL Server).
Installation
composer require microweber-packages/microweber-searchable
Usage
Add the HasSearchableTrait to your Eloquent model and define which fields are searchable:
use MicroweberPackages\Searchable\HasSearchableTrait; class User extends Model { use HasSearchableTrait; protected $searchable = [ 'email', 'username', 'first_name', 'last_name', ]; // Optional: define separate fields for keyword search protected $searchableByKeyword = [ 'first_name', 'last_name', ]; }
Searching
// Search using LIKE across keyword-searchable fields User::search('john')->get(); // Search specific fields User::search('john', ['first_name', 'last_name'])->get(); // Exact match on a searchable field User::searchExact('email', 'john@example.com')->first(); // Check if a field is searchable $user = new User(); $user->isSearchableField('email'); // true $user->isSearchableField('password'); // false
API
| Method | Description |
|---|---|
getSearchable() |
Returns the list of searchable fields |
getSearchableByKeyword() |
Returns keyword-search fields (falls back to $searchable) |
scopeSearch($query, $keyword, $fields) |
LIKE search scope |
scopeSearchExact($query, $field, $value) |
Exact match scope |
isSearchableField($field) |
Check if a field is in the searchable list |
License
MIT