oscabrera/laravel-query-filters

A Laravel package for advanced query filtering and conditions.

v1.0.2 2024-12-28 14:26 UTC

This package is auto-updated.

Last update: 2025-01-29 14:14:13 UTC


README

A Laravel package for advanced query filtering and conditions.

Latest Version on Packagist Total Downloads

VitePress PHPStan Pint PHPMD

built with Codeium

laravel-query-filters

Installation

Install the package via Composer:

composer require oscabrera/laravel-query-filters

Configuration (Optional)

You can publish the configuration file to customize the method name used for applying filters. This is optional, but it provides additional flexibility.

Publish the configuration file:

php artisan vendor:publish --provider="Oscabrera\QueryFilters\QueryFiltersServiceProvider" --tag="config"

This will create a config/query-filters.php file in your Laravel application. You can modify this file to change the method name:

return [
    /*
     * The name of the macro that is added to the Eloquent query builder.
     */
    'method_name' => 'applyFilters',
];

Usage

Here's an example of how to use the QueryFilters in a controller:

use Oscabrera\QueryFilters\Utilities\QueryFilters;
use App\Models\User;
use Illuminate\Http\Request;

public function index(Request $request)
{
    $conditions = [
        'name' => 'John',
        'age' => ['age', '>', 25],
        'status' => ['active', 'pending'],
        'role' => [
            'subQuery' => function($query) {
                return $query->select('id') ->from('roles') ->where('name', 'admin');
            },
            'not' => false,
        ]
    ];
    $queryFilters = new QueryFilters($conditions);
    $users = User::query()->applyFilters($queryFilters)->get();

    return response()->json($users);
}

Features

  • Simple Conditions: Apply simple equality conditions.
  • Array Conditions: Use whereIn conditions for columns.
  • Triple Conditions: Apply conditions with specific operators.
  • SubQueries: Use subqueries for complex filtering.
  • Customizable Method Name: Change the method name for applying filters via configuration.

More Information

For more information, visit the documentation.

By following these instructions, you can easily integrate advanced query filtering into your Laravel applications, customize the filtering method name, and keep your code clean and maintainable.