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

v3.0.0 2025-11-06 07:44 UTC

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

Packagist Version License PHP Version Downloads

Created with โค๏ธ by Daiyan Mozumder

Features โ€ข Installation โ€ข Quick Start โ€ข Documentation โ€ข Examples โ€ข Contributing

๐Ÿ“‘ Table of Contents

โœจ 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!


GitHub stars GitHub forks


Made with โค๏ธ by Daiyan Mozumder

Empowering Laravel developers with flexible search solutions