You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Filterable Package** is a Laravel package designed to simplify filtering Eloquent models in your application. It provides an easy-to-use interface to apply various types of filters to your Eloquent queries, such as exact matches, partial matches (LIKE), date ranges, JSON-based filters, and more, directly from incoming requests. This package is especially useful for building APIs where dynamic filtering is often required.
3
+
A Laravel package for filterable traits and classes. This package provides powerful, dynamic query filtering capabilities directly from incoming requests, especially useful when developing flexible and dynamic APIs.
4
4
5
5
## Features
6
-
-**Easy Integration:** Apply the `Filterable` trait to your Eloquent models to start using it.
7
-
-**Multiple Filters:** Supports a variety of filters such as exact matches, LIKE searches, greater than, less than, IN clauses, JSON-based filters, and date ranges.
8
-
-**Custom Filter Mapping:** Easily map request parameters to different column names in the database.
9
-
-**Flexible Sorting:** Allows for dynamic sorting of results based on request parameters.
10
6
11
-
## Installation
7
+
-**Easy Integration:** Apply the `Filterable` trait to your Eloquent models.
8
+
-**Flexible Filters:** Exact, like, in, between, greater than (gte, gt), less than (lte, lt), JSON, and relationship filters.
9
+
-**Dynamic Sorting:** Customize sorting behavior directly from requests.
10
+
-**Relationship Filters:** Use advanced conditional logic like `whereAny`, `whereAll`, and `whereNone` for relational queries.
11
+
-**JSON Support:** Directly filter JSON columns with dot-notation.
-**Description:** Filters records where the column value matches exactly with the provided value.
79
-
-**Example:**`Filter::exact('status', 'status')` filters records where the `status` column matches the value of the `status` parameter in the request.
80
-
81
-
### `Filter::like($attribute, $filterBy = null)`
82
-
-**Description:** Filters records where the column value is similar to the provided value (uses SQL `LIKE`).
83
-
-**Example:**`Filter::like('name', 'search')` filters records where the `name` column contains the value of the `search` parameter in the request.
84
-
85
-
### `Filter::in($attribute, $filterBy = null)`
86
-
-**Description:** Filters records where the column value is within a specified list of values.
87
-
-**Example:**`Filter::in('category_id', 'categories')` filters records where the `category_id` column matches any value provided in the `categories` parameter in the request.
88
-
89
-
### `Filter::gte($attribute, $filterBy = null)`
90
-
-**Description:** Filters records where the column value is greater than or equal to the provided value.
91
-
-**Example:**`Filter::gte('amount', 'min_amount')` filters records where the `amount` column is greater than or equal to the value of the `min_amount` parameter in the request.
92
-
93
-
### `Filter::lte($attribute, $filterBy = null)`
94
-
-**Description:** Filters records where the column value is less than or equal to the provided value.
95
-
-**Example:**`Filter::lte('amount', 'max_amount')` filters records where the `amount` column is less than or equal to the value of the `max_amount` parameter in the request.
-**Description:** Filters records based on JSON attributes. Allows querying nested JSON data using a dot-notated path.
99
-
-**Parameters:**
100
-
-`$attribute`: The column that contains the JSON data.
101
-
-`$path`: The dot-notated path to the JSON attribute (e.g., `'user.name'`).
102
-
-`$operator`: The comparison operator (e.g., `'='`, `'LIKE'`, `'>'`, etc.).
103
-
-`$filterBy`: The request parameter name to map to this filter.
104
-
-**Example:**
105
-
-`Filter::json('attributes', 'user.name', 'LIKE', 'user_name')` filters records where the `user.name` attribute in the `attributes` JSON column matches the `user_name` request parameter using a `LIKE` comparison.
106
-
-`Filter::json('attributes', 'user.age', '>', 'user_age')` filters records where the `user.age` attribute in the `attributes` JSON column is greater than the `user_age` request parameter.
-**Description:** Filters records where the column value is within a specified range (inclusive).
110
-
-**Parameters:**
111
-
-`$attribute`: The database column to apply the filter on.
112
-
-`$filterBy`: (Optional) The request parameter name to map to this filter. Defaults to the attribute name.
113
-
-**Example Usage:**
114
-
-`Filter::between('expense_date', 'date_range')` filters records where the `expense_date` column falls between two values provided in the `date_range` parameter in the request.
115
-
116
-
**API Request Example:**
117
-
`` Get /api/expenses?filter[date_range]=2023-01-01,2023-12-31
118
-
```
119
-
120
-
## Custom Filter Mapping
121
-
You can map request parameters to different column names in your database. For example:
Use the provided methods to paginate and sort easily:
122
108
123
109
```php
124
-
protected array $filterMap = [
125
-
'search' => 'description',
126
-
'date' => 'expense_date',
127
-
];
128
-
```
129
-
130
-
Now, if the request contains `filter[search]=Pizza`, the query will filter the `description` column for the value `Pizza`.
131
-
132
-
## Benefits and Differentiators
133
-
134
-
-**Enhanced Code Readability:** Filters are applied in a clean, readable manner without cluttering your controllers or models with complex query logic.
135
-
-**Dynamic Filtering:** Easily handle multiple filters from a single request without manually parsing input.
136
-
-**Reusability:** Filters can be reused across different queries and models, making your code DRY (Don't Repeat Yourself).
137
-
-**Flexible Sorting:** Allows sorting results dynamically based on request parameters, ensuring that your API remains flexible to client needs.
138
-
-**JSON Support:** Easily filter nested JSON attributes, enhancing the flexibility of your queries when dealing with JSON data types.
Now, using the parameter `filter[display_name]=John` will filter on the `name` column.
203
136
204
-
The package supports JSON-based filters across different database systems by handling the JSON extraction appropriately.
137
+
## Supported Databases for JSON Filters
138
+
- MySQL
139
+
- PostgreSQL
140
+
- SQLite
205
141
206
-
### Supported Databases:
207
-
-**MySQL:** Uses the `->>` operator to extract JSON values.
208
-
-**SQLite:** Uses the `json_extract` function.
209
-
-**PostgreSQL:** Uses the `->>` operator to extract JSON values.
210
-
211
-
Ensure that your database driver is correctly set to utilize the appropriate JSON extraction method.
212
-
213
-
## Conclusion
214
-
215
-
The **Filterable Package** simplifies the process of filtering Eloquent models in Laravel. By leveraging the power of this package, you can create highly flexible and dynamic filtering solutions in your application, improving code quality and maintainability. With support for JSON-based filters, you can efficiently query nested JSON attributes, making your APIs more robust and versatile.
142
+
The package automatically detects the database driver from your configuration.
216
143
217
144
## License
218
145
219
-
This package is open-sourced software licensed under the [MIT license](LICENSE).
220
-
```
146
+
This package is open-sourced under the MIT license.
0 commit comments