soliudeen999/laravel-query-filter

A powerful and flexible query filter system for Laravel applications

Maintainers

Package info

github.com/Soliudeen999/laravel-query-filter

pkg:composer/soliudeen999/laravel-query-filter

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2025-11-28 09:38 UTC

This package is auto-updated.

Last update: 2026-02-28 10:19:57 UTC


README

A powerful and flexible query filter and search system for Laravel applications that allows you to easily filter and search your Eloquent models based on request parameters.

Installation

You can install the package via composer:

composer require soliudeen999/laravel-query-filter

Setup

Add the HasFilter trait to your model and define the filterable fields:

use Soliudeen999\QueryFilter\Traits\HasFilter;

class User extends Model
{
    use HasFilter;

    protected array $filterables = [
        'name',
        'email',
        'status',
        'role' => ['admin', 'user', 'guest'],
        'posts' => 'posts:title',
        'withTrashed' => ['with', 'only']
    ];
}

Usage

Basic Filtering

// Filter using request parameters automatically
$users = User::filter()->get();

// Filter with specific parameters
$users = User::filter(['status' => 'active'])->get();

// Filter multiple fields
$users = User::filter([
    'status' => 'active',
    'role' => 'admin'
])->get();

Advanced Filtering

Comparison Operators

The following operators are supported:

  • gt (greater than)
  • lt (less than)
  • eq (equals)
  • neq (not equals)
  • gte (greater than or equal)
  • lte (less than or equal)
  • btw (between)
  • in (in array)
// Using operators
$users = User::filter([
    'age' => ['gt' => 18, 'lt' => 65],
    'status' => ['in' => ['active', 'pending']],
    'price' => ['btw' => [100, 200]],
    'rating' => ['gte' => 4.5]
])->get();

Relationship Filtering

Define relationship filters in your $filterables array:

protected array $filterables = [
    'posts' => 'posts:relationship,title', // format: 'table:relationship,column'
    'comments' => 'comments:relationship,content'
];

// Usage
$users = User::filter([
    'posts' => 'Laravel', // Find users with posts containing 'Laravel' in title
    'comments' => ['active'] // Find users with these comment types
])->get();

Special Values Filtering

Define allowed values in your $filterables array:

protected array $filterables = [
    'role' => ['admin', 'user', 'guest'],
    'status' => ['active', 'inactive']
];

// Usage
$users = User::filter([
    'role' => 'admin',  // Will only filter if 'admin' is in allowed values
    'status' => 'active'
])->get();

Setup

Add the HasSearch trait to your model and define the searchable fields:

use Soliudeen999\QueryFilter\Traits\HasFilter;

class User extends Model
{
    use HasSearch;

    protected array $searchable = [
        'name',
        'email',
        'status',
        'posts:name'
    ];
}

Usage

Basic Filtering

// Filter using request parameters automatically
$users = User::search('what to look for')->get();

// Filter with specific parameters
$users = User::search('what to look for')->get();

// Filter multiple fields
$users = User::search('what to look for')->get();

Request Parameters

When using request parameters, you can filter using query string parameters:

/users?name=John&status=active&age[gt]=18&age[lt]=65&sort=-created_at

Best Practices

  1. Always define the $filterables array in your model to specify which fields can be filtered
  2. Use type hints and validation where possible
  3. Keep the filter values consistent with your database schema
  4. Use relationship filters for complex queries
  5. Implement custom search scopes for specific search requirements

Error Handling

The package will throw exceptions for:

  • Missing $filterables property
  • Invalid relationship filter configurations
  • Invalid operator usage
  • Invalid between clause values

Make sure to handle these exceptions appropriately in your application.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email soliudeen999@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.