A Laravel package for advanced query filtering and conditions.
Install the package via Composer:
composer require oscabrera/laravel-query-filters
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',
];
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);
}
- 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.
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.