Skip to content

Commit 3ca5c86

Browse files
docs(README): update package description and features
1 parent 557eb21 commit 3ca5c86

File tree

2 files changed

+78
-159
lines changed

2 files changed

+78
-159
lines changed

README.md

Lines changed: 77 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
# Filterable Package for Laravel
1+
# Filterable Package
22

3-
**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.
44

55
## 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.
106

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.
1212

13-
To install the package, use Composer:
13+
## Installation
1414

1515
```bash
1616
composer require devaction-labs/filterable-package
1717
```
1818

1919
## Usage
2020

21-
### Step 1: Apply the `Filterable` Trait to Your Model
22-
23-
Apply the `Filterable` trait to any Eloquent model you want to filter.
21+
### Step 1: Add the `Filterable` Trait
2422

2523
```php
2624
namespace App\Models;
@@ -32,189 +30,117 @@ class Expense extends Model
3230
{
3331
use Filterable;
3432

35-
// Define any custom filter map or allowed sorts if necessary
3633
protected array $filterMap = [
37-
'search' => 'description', // Example: map 'search' request parameter to 'description' column
34+
'search' => 'description',
35+
'date' => 'expense_date',
3836
];
3937

40-
protected array $allowedSorts = ['expense_date', 'amount']; // Example: allow sorting by these columns
38+
protected array $allowedSorts = ['expense_date', 'amount'];
4139
}
4240
```
4341

44-
### Step 2: Apply Filters in Your Controller
45-
46-
Apply filters in your controller methods using the `filtrable` scope provided by the `Filterable` trait.
42+
### Step 2: Applying Filters in Controllers
4743

4844
```php
49-
namespace App\Http\Controllers\Api\Finance;
45+
namespace App\Http\Controllers\Api;
5046

5147
use App\Http\Controllers\Controller;
52-
use App\Http\Resources\ExpenseCollection;
53-
use App\Models\Expense;
5448
use DevactionLabs\FilterablePackage\Filter;
49+
use App\Models\Expense;
5550

5651
class ExpenseController extends Controller
5752
{
58-
public function index(): ExpenseCollection
53+
public function index()
5954
{
6055
$expenses = Expense::query()
61-
->with(['category', 'period'])
6256
->filtrable([
6357
Filter::like('description', 'search'),
6458
Filter::exact('expense_date', 'date'),
59+
Filter::between('expense_date', 'date_range'),
6560
Filter::json('attributes', 'user.name', 'LIKE', 'user_name'),
6661
Filter::json('attributes', 'user.age', '>', 'user_age'),
67-
])
68-
->customPaginate();
69-
70-
return new ExpenseCollection($expenses);
62+
Filter::relationship('user', 'name')->setValue('John')->with(),
63+
Filter::relationship('user', 'name')->whereAny([
64+
['name', '=', 'John'],
65+
['email', '=', 'john@example.com'],
66+
])->with(),
67+
->customPaginate(false, ['per_page' => 10, 'sort' => '-created_at']);
68+
69+
return response()->json($expenses);
7170
}
7271
}
7372
```
7473

7574
## Available Filters
7675

77-
### `Filter::exact($attribute, $filterBy = null)`
78-
- **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.
96-
97-
### `Filter::json($attribute, $path, $operator = '=', $filterBy = null)`
98-
- **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.
107-
### `Filter::between($attribute, $filterBy = null)`
108-
109-
- **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:
76+
### Direct Filters
77+
- **Exact Match:** `Filter::exact('status', 'status')`
78+
- **LIKE Match:** `Filter::like('description', 'search')`
79+
- **IN Clause:** `Filter::in('category_id', 'categories')`
80+
- **Greater Than or Equal:** `Filter::gte('amount', 'min_amount')`
81+
- **Less Than or Equal:** `Filter::lte('amount', 'max_amount')`
82+
- **Between:** `Filter::between('created_at', 'date_range')`
83+
84+
### JSON Filters
85+
- **Exact Match:** `Filter::json('data', 'user.name', '=', 'user_name')`
86+
- **LIKE Match:** `Filter::json('data', 'user.name', 'LIKE', 'user_name')`
87+
88+
### Relationship Filters
89+
- **Simple Relationship:**
90+
```php
91+
Filter::relationship('user', 'name')->setValue('John')->with()
92+
```
93+
94+
- **Conditional Logic (`whereAny`, `whereAll`, `whereNone`):**
95+
```php
96+
Filter::relationship('user', 'name')
97+
->whereAny([
98+
['name', '=', 'John'],
99+
['email', '=', 'john@example.com'],
100+
])
101+
->setValue('John')
102+
->with();
103+
```
104+
105+
## Customizing Pagination and Sorting
106+
107+
Use the provided methods to paginate and sort easily:
122108

123109
```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.
139-
140-
## Example Usage in API Controller
141-
142-
```php
143-
namespace App\Http\Controllers\Api\Finance;
144-
145-
use App\Http\Controllers\Controller;
146-
use App\Http\Resources\ExpenseCollection;
147-
use App\Models\Expense;
148-
use DevactionLabs\FilterablePackage\Filter;
149-
150-
class ExpenseController extends Controller
151-
{
152-
public function index(): ExpenseCollection
153-
{
154-
$expenses = Expense::query()
155-
->with(['category', 'period'])
156-
->filtrable([
157-
Filter::like('description', 'search'),
158-
Filter::exact('expense_date', 'date'),
159-
Filter::between('expense_date', 'date_range'),
160-
Filter::json('attributes', 'user.name', 'LIKE', 'user_name'),
161-
Filter::json('attributes', 'user.age', '>', 'user_age'),
162-
])
163-
->customPaginate();
164-
165-
return new ExpenseCollection($expenses);
166-
}
167-
}
110+
$results = Expense::query()
111+
->filtrable([...])
112+
->customPaginate(false, ['per_page' => 10, 'sort' => '-created_at']);
168113
```
169114

170-
## Pagination and Sorting
115+
- `-` (minus) prefix indicates descending sorting (e.g., `-amount`).
171116

172-
### Custom Pagination
173-
174-
Use the `customPaginate` scope to apply pagination with customizable parameters.
117+
### Defining Default Sorting and Allowed Sorts in Model:
175118

176119
```php
177-
$expenses = Expense::query()
178-
->filtrable([...])
179-
->customPaginate(); // Defaults to 15 items per page
180-
181-
// With custom parameters
182-
$expenses = Expense::query()
183-
->filtrable([...])
184-
->customPaginate(false, ['per_page' => 10, 'sort' => '-amount']);
120+
protected string $defaultSort = 'amount';
121+
protected array $allowedSorts = ['amount', 'expense_date'];
185122
```
186123

187-
### Allowed Sorts and Default Sort
124+
## Custom Filter Mapping
188125

189-
Define allowed sorts and a default sort order in your model.
126+
Easily map request parameters to database columns:
190127

191128
```php
192-
protected array $allowedSorts = ['expense_date', 'amount'];
193-
protected string $defaultSort = 'expense_date';
194-
195-
// In your controller
196-
$expenses = Expense::query()
197-
->filtrable([...])
198-
->allowedSorts(['expense_date', 'amount'])
199-
->customPaginate();
129+
protected array $filterMap = [
130+
'display_name' => 'name',
131+
'date' => 'expense_date',
132+
];
200133
```
201134

202-
## Handling JSON Filters with Different Databases
135+
Now, using the parameter `filter[display_name]=John` will filter on the `name` column.
203136

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
205141

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.
216143

217144
## License
218145

219-
This package is open-sourced software licensed under the [MIT license](LICENSE).
220-
```
146+
This package is open-sourced under the MIT license.

composer.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"email": "alex@devaction.com.br"
1010
}
1111
],
12-
"version": "v1.0.19",
12+
"version": "v1.0.20",
1313
"require": {
1414
"php": "^8.2|^8.3|^8.4",
1515
"illuminate/cache": "^12.2",
@@ -32,13 +32,6 @@
3232
"DevactionLabs\\FilterablePackage\\": "src/"
3333
}
3434
},
35-
"extra": {
36-
"laravel": {
37-
"providers": [
38-
"DevactionLabs\\FilterablePackage\\FilterableServiceProvider"
39-
]
40-
}
41-
},
4235
"autoload-dev": {
4336
"psr-4": {
4437
"Tests\\": "tests/"

0 commit comments

Comments
 (0)