daiyanmozumder / laravel-flexsearch
A flexible Laravel search and filter helper for Eloquent models.
Installs: 3
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/daiyanmozumder/laravel-flexsearch
Requires
- php: >=8.0
- illuminate/support: ^10.0|^11.0|^12.0
This package is auto-updated.
Last update: 2026-01-09 09:57:00 UTC
README
๐ Laravel FlexSearch
Powerful Dynamic Filtering, Relationship & Keyword Search for Laravel Eloquent
Created with โค๏ธ by Daiyan Mozumder
Features โข Installation โข Quick Start โข Documentation โข Examples โข Contributing
๐ Table of Contents
- โจ Features
- ๐ฆ Installation
- โก Quick Start
- ๐งพ Documentation
- ๐ง How Keyword Search Works
- ๐ Relationship Filtering & Search
- ๐ก Usage Examples
- ๐ Benefits
- ๐ค Contributing
- ๐ License
- ๐ฎ Roadmap
โจ Features
| ๐ฏ Dynamic Filters | ๐ Keyword Search | ๐ Relationship Aware |
|---|---|---|
Apply simple or operator-based filters (=, >, <, >=, !=) for flexible database queries |
Perform powerful full-text-like search across multiple model columns โ even in relationships! | Supports filtering and searching through related models using dot notation (e.g. company.name) |
๐ Zero Configuration Required
๐ No service provider or config needed โ works instantly out of the box!
๐ฆ Installation
Install via Composer:
composer require daiyanmozumder/laravel-flexsearch
๐ก No additional setup needed! The package is ready to use immediately after installation.
โก Quick Start
use DaiyanMozumder\LaravelFlexSearch\FlexSearch; use App\Models\User; use Illuminate\Http\Request; class UserController extends Controller { public function index(Request $request, FlexSearch $flexSearch) { $query = User::query()->with('company'); $filters = $request->only(['status', 'company.name']); $searchTerm = $request->input('q'); $searchable = ['name', 'email', 'company.name']; $users = $flexSearch->apply($query, $filters, $searchTerm, $searchable) ->paginate(15); return view('users.index', compact('users')); } }
โ That's it! You now have dynamic filters, keyword search, and relational querying in one powerful line.
๐งพ Documentation
Method Signature
public function apply( Builder $query, array $filters = [], ?string $searchTerm = null, array $searchableColumns = [] ): Builder
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
$query |
Builder |
โ | Eloquent query builder instance |
$filters |
array |
โ | Dynamic key-value filters with optional operators |
$searchTerm |
?string |
โ | Keyword(s) for text-based search |
$searchableColumns |
array |
โ | Columns (including relation columns) to search |
๐งฉ Operator-Based Filtering
FlexSearch supports powerful operator-based filtering:
$filters = [ 'price>=' => 100, 'created_at!=' => '2024-01-01', 'status' => 'active', ];
Generated SQL:
WHERE price >= 100 AND created_at != '2024-01-01' AND status = 'active'
๐ Relationship Filtering & Search
You can filter or search within related models using dot notation for seamless relationship querying.
๐ Example 1: Filtering on Relationships
$filters = [ 'company.name=' => 'Ashlar Tech', 'status' => 'active' ]; $query = User::with('company'); $flexSearch->apply($query, $filters)->get();
Generated SQL (simplified):
WHERE EXISTS ( SELECT * FROM companies WHERE users.company_id = companies.id AND companies.name = 'Ashlar Tech' ) AND users.status = 'active'
๐ Example 2: Searching on Related Columns
$searchTerm = 'daiyan ashlar'; $searchableColumns = ['name', 'email', 'company.name']; $query = User::with('company'); $flexSearch->apply($query, [], $searchTerm, $searchableColumns)->get();
Result: Finds users where name, email, or company.name matches any search term.
๐ง How Keyword Search Works
FlexSearch splits your input into words, then applies smart logic:
AND between words โ every term must match
OR between columns โ each term can match any field
Example:
$searchTerm = "red sport"; $columns = ['title', 'description'];
Generated Query:
WHERE ( (title LIKE '%red%' OR description LIKE '%red%') AND (title LIKE '%sport%' OR description LIKE '%sport%') )
๐ก Usage Examples
๐ Example 1: Product Search
$products = (new FlexSearch())->apply( Product::query(), ['category_id' => 3, 'price>=' => 100], 'cotton tshirt', ['name', 'description', 'brand.name'] )->get();
๐ฅ Example 2: User Search with Relationships
$query = User::query()->with('company'); $users = (new FlexSearch())->apply( $query, ['company.name=' => 'Ashlar Tech', 'status' => 'active'], 'daiyan', ['name', 'email', 'company.name'] )->paginate(10);
๐ฐ Example 3: Blog Post Search
$posts = (new FlexSearch())->apply( Post::with('author', 'tags'), ['category_id' => $request->category], $request->search, ['title', 'body', 'author.name', 'tags.name'] )->paginate(15);
๐ Benefits
| Feature | Description |
|---|---|
| ๐ Zero Setup | Works instantly, no config files required |
| ๐ง Highly Flexible | Handles filters, relations, and keywords with ease |
| โก Optimized Queries | Uses whereHas intelligently for better performance |
| ๐ก Readable Syntax | Expressive, minimal, and clean API |
| ๐งฑ ORM Friendly | Seamlessly integrates with Eloquent relationships |
| ๐ Chainable | Works perfectly with query chains and other builders |
๐ค Contributing
We welcome contributions! ๐
Pull requests are welcome on GitHub.
Guidelines:
- โ Follow PSR-12 coding standards
- โ Add tests where applicable
- โ Keep commits meaningful and scoped
- โ Document all new features
๐ Join our community of contributors!
๐ License
The MIT License (MIT). See LICENSE.md for details.
๐ Credits
Created & Maintained by
Daiyan Mozumder
Special thanks to all contributors who help make this project better! ๐
๐ฎ Roadmap
| Status | Feature |
|---|---|
| โ | Relationship-based search |
| โ | Operator-based filtering (>, <, >=, !=) |
| ๐ง | BETWEEN and IN support |
| ๐ง | Fuzzy search and match ranking |
| ๐ | Result highlighting |
| ๐ | Query caching |
๐ Show Your Support
If you find this package helpful, please โญ star it on GitHub!
Made with โค๏ธ by Daiyan Mozumder
Empowering Laravel developers with flexible search solutions