Download the PHP package mohamedfathy/dynamic-filters without Composer
On this page you can find all versions of the php package mohamedfathy/dynamic-filters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mohamedfathy/dynamic-filters
More information about mohamedfathy/dynamic-filters
Files in mohamedfathy/dynamic-filters
Package dynamic-filters
Short Description A Laravel package that provides dynamic filtering, sorting, and relationship-based querying for Eloquent models.
License MIT
Informations about the package dynamic-filters
Dynamic API Query Filters for Laravel Applications
A lightweight and flexible query filtering package for Laravel, supporting standard filters, relationship filters, and custom filters via URL parameters. It enables sorting and pagination effortlessly while keeping query logic clean and maintainable. With intuitive syntax and extendability, it simplifies API request handling for scalable applications.
🚀 Basic Usage
Filter a query based on a request:
Example Request
SQL:
Adding Filters to the Product Model
Usage in Controller:
🛠 Installation
Install via Composer:
📌 List of Available Operators
Below are the available operators you can use in filtering:
| Operator | SQL Equivalent | Description | Example Usage |
|---|---|---|---|
eq |
= |
Equal to | filters[status:eq]=active |
neq |
!= |
Not equal to | filters[status:neq]=active |
gt |
> |
Greater than | filters[price:gt]=100 |
lt |
< |
Less than | filters[price:lt]=1000 |
gte |
>= |
Greater than or equal | filters[rating:gte]=4 |
lte |
<= |
Less than or equal | filters[discount:lte]=50 |
like |
LIKE |
Partial match | filters[name:like]=car |
nLike |
NOT LIKE |
Does not match pattern | filters[name:nLike]=tablet |
null |
IS NULL |
Field is null | filters[deleted_at:null] |
nNull |
IS NOT NULL |
Field is not null | filters[updated_at:nNull] |
in |
IN |
Value in list | filters[status:in]=active,pending |
nIn |
NOT IN |
Value not in list | filters[status:nIn]=banned |
between |
BETWEEN |
Value in range | filters[price:between]=100,500 |
nBetween |
NOT BETWEEN |
Value outside range | filters[age:nBetween]=18,60 |
regexp |
REGEXP |
Matches regex pattern | filters[sku:regexp]=^[A-Z]+ |
nRegexp |
NOT REGEXP |
Does not match regex pattern | filters[code:nRegexp]=[0-9]+ |
🔍 Query Examples
✅ Standard Filtering
Request:
SQL:
✅ Multiple Filters
Request:
SQL:
✅ Sorting
Request:
SQL:
✅ Pagination
Request:
SQL:
✅ Filtering by Relationship
Request:
SQL:
✅ Custom Filter Example
Request:
SQL:
🔧 Why Use Custom Filters?
By default, standard filters and relation filters cover most scenarios. However, in some cases, you may need more flexibility.
For example, if your filtering logic involves complex conditions, multiple values, or custom operations, it's best to create a dedicated Filter Class
for each model and handle all related filters inside it.
How to Create Custom Filters
Create a Filter Class for Each Model
Instead of writing filtering logic inside controllers or query scopes, you should create a dedicated filter class for each model.
For example, let's create a ProductFilters class that handles filtering for the Product model.
✅ Conclusion
- Simple API for filtering, sorting, and relationship handling
- Customizable filters for advanced use cases
- Efficient query execution
📌 Need Help?
Feel free to contribute or raise an issue if you have suggestions! 🚀