-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oscar Cabrera
committed
Jun 11, 2024
1 parent
8edefcf
commit a2e2c7b
Showing
4 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Usage Example | ||
|
||
```php | ||
$conditions = [ | ||
'rate' => [ | ||
'subQuery' => function ($query) use (array $excluded) { | ||
$subQuery = $query->from('reviews as a')->selectRaw('min(a.rate)')->where('a.active', true); | ||
if($excluded) { | ||
$subQuery->whereNotIn('a.name', $excluded); | ||
} | ||
return $subQuery; | ||
}, | ||
'not' => false, | ||
], | ||
['name', '<>', $excluded], | ||
'active' => true | ||
]; | ||
|
||
$queryFilters = new QueryFilters($conditions); | ||
$users = User::query()->applyFilters($query)->get(); | ||
``` | ||
|
||
This example defines a `$conditions` array with various filtering conditions. Then, an instance of the `QueryFilters` | ||
class | ||
is created and applied to the query builder object using the `apply` method. Finally, the query is executed to | ||
retrieve the users that meet the specified conditions. | ||
|
||
## Simple Conditions. | ||
|
||
```php | ||
use Oscabrera\QueryFilters\Utilities\QueryFilters; | ||
use App\Models\YourModel; | ||
use Illuminate\Http\Request; | ||
|
||
public function index(Request $request) | ||
{ | ||
$conditions = ['status' => 'active']; | ||
$queryFilters = new QueryFilters($conditions); | ||
$results = YourModel::query()->applyFilters($queryFilters)->get(); | ||
|
||
return response()->json($results); | ||
} | ||
``` | ||
## Array Conditions. | ||
|
||
```php | ||
use Oscabrera\QueryFilters\Utilities\QueryFilters; | ||
use App\Models\YourModel; | ||
use Illuminate\Http\Request; | ||
|
||
public function index(Request $request) | ||
{ | ||
$conditions = ['status' => ['active', 'pending']]; | ||
$queryFilters = new QueryFilters($conditions); | ||
$results = YourModel::query()->applyFilters($queryFilters)->get(); | ||
|
||
return response()->json($results); | ||
} | ||
``` | ||
|
||
## Triple Conditions Simple. | ||
|
||
```php | ||
use Oscabrera\QueryFilters\Utilities\QueryFilters; | ||
use App\Models\YourModel; | ||
use Illuminate\Http\Request; | ||
|
||
public function index(Request $request) | ||
{ | ||
$queryFilters = new QueryFilters([['age', '>', 18]]); | ||
$results = YourModel::query()->applyFilters($queryFilters)->get(); | ||
|
||
return response()->json($results); | ||
} | ||
``` | ||
|
||
## Triple Conditions Matrix. | ||
|
||
```php | ||
use Oscabrera\QueryFilters\Utilities\QueryFilters; | ||
use App\Models\YourModel; | ||
use Illuminate\Http\Request; | ||
|
||
public function index(Request $request) | ||
{ | ||
$queryFilters = new QueryFilters([['age', '=', [18, 19, 20]]]); | ||
$results = YourModel::query()->applyFilters($queryFilters)->get(); | ||
|
||
return response()->json($results); | ||
} | ||
``` | ||
|
||
## SubQueries. | ||
|
||
```php | ||
use Oscabrera\QueryFilters\Utilities\QueryFilters; | ||
use App\Models\YourModel; | ||
use Illuminate\Http\Request; | ||
|
||
public function index(Request $request) | ||
{ | ||
$conditions = [ | ||
'user_id' => [ | ||
'subQuery' => function($query) { | ||
$query->select('id')->from('users')->where('active', 1); | ||
} | ||
] | ||
]; | ||
$queryFilters = new QueryFilters($conditions); | ||
$results = YourModel::query()->applyFilters($queryFilters)->get(); | ||
|
||
return response()->json($results); | ||
} | ||
``` | ||
|
||
## Negative SubQueries. | ||
|
||
```php | ||
use Oscabrera\QueryFilters\Utilities\QueryFilters; | ||
use App\Models\YourModel; | ||
use Illuminate\Http\Request; | ||
|
||
public function index(Request $request) | ||
{ | ||
$conditions = [ | ||
'user_id' => [ | ||
'subQuery' => function($query) { | ||
$query->select('id')->from('users')->where('active', 0); | ||
}, | ||
'not' => true | ||
] | ||
]; | ||
$queryFilters = new QueryFilters($conditions); | ||
|
||
$results = YourModel::query() | ||
->applyFilters($queryFilters) | ||
->get(); | ||
|
||
return response()->json($results); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Filter Types | ||
|
||
There are six types of filtering. | ||
|
||
## List of Filter Types | ||
|
||
- Simple Conditions. | ||
- Array Conditions. | ||
- Triple Conditions Simple. | ||
- Triple Conditions Matrix. | ||
- SubQueries. | ||
- Negative SubQueries. | ||
|
||
## Filters Description | ||
|
||
### Simple Conditions | ||
|
||
**Description**: Applies a simple equality condition to a column. | ||
|
||
**Example**: | ||
```php | ||
['status' => 'active'] | ||
``` | ||
- This will filter out records where the status column equals 'active'. | ||
|
||
### Matrix Conditions | ||
|
||
**Description**: Applies a **WHERE IN** condition to a column, checking if the column value is within a list of values. | ||
|
||
**Example**: | ||
```php | ||
['status' => ['active', 'pending']] | ||
``` | ||
- This will filter out records where the status column is `'active'` or `'pending'`. | ||
|
||
### Triple Conditions Simple | ||
|
||
**Description**: Applies a condition with a specific operator to a column. | ||
|
||
**Example**: | ||
```php | ||
[['age', '>', 18]] | ||
``` | ||
- This will filter out records where the age column is greater than `18`. | ||
|
||
> The logic of the triple conditions in the QueryFilters class closely follows the where clause approach in Laravel, as | ||
described in the official Laravel documentation. Just like in Laravel where clauses, where you can specify the column, | ||
operator and value for the conditions. more info [here](https://laravel.com/docs/11.x/queries#where-clauses). | ||
|
||
### Triple Conditions Matrix | ||
|
||
**Description**: Applies a condition with a specific operator to a column using **WHERE IN** or **WHERE NOT IN**. | ||
|
||
**Example**: | ||
```php | ||
[['age', '=', [18, 19, 20]]] | ||
``` | ||
- This will filter out records where the age column is equal to `18`, `19`, or `20`. | ||
|
||
### SubQueries | ||
|
||
**Description**: Applies a **WHERE IN** condition using a **subquery**. | ||
|
||
**Example**: | ||
```php | ||
[ | ||
'user_id' => [ | ||
'subQuery' => function($query) { | ||
return $query->select('id')->from('users')->where('active', 1); | ||
}, | ||
'not' => false, | ||
] | ||
] | ||
``` | ||
- This will filter out records where the `user_id` column is in the list of active user IDs. | ||
> the parameter `not` is optional and defaults to `false`. | ||
### Negative SubQueries | ||
|
||
**Description**: Applies a **WHERE NOT IN** condition using a **subquery**. | ||
|
||
**Example**: | ||
```php | ||
[ | ||
'user_id' => [ | ||
'subQuery' => function($query) { | ||
return $query->select('id')->from('users')->where('active', 0); | ||
}, | ||
'not' => true, | ||
] | ||
] | ||
``` | ||
|
||
- This will filter out records where the `user_id` column is not in the list of inactive user IDs. | ||
Simple Examples of Use |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# QueryFilters Structure | ||
|
||
## Constructor | ||
|
||
The constructor for the QueryFilters class takes two optional arguments: | ||
|
||
conditions: An associative array that defines the filtering conditions for the query. (optional, defaults to []) | ||
|
||
```php | ||
public function __construct(array $conditions = []) | ||
``` | ||
|
||
## Methods | ||
|
||
### Public Methods | ||
|
||
**apply** (Builder $query): This method applies the conditions defined in the conditions property to the query | ||
builder object. | ||
|
||
### Protected Methods | ||
|
||
**handleArrayCondition** (Builder $query, string|int $key, array $condition): This internal method handles filtering | ||
conditions that are arrays. It analyzes the format of the array and applies the appropriate condition to the query | ||
builder object. | ||
|
||
**handleTripleCondition** (Builder $query, array $condition): This internal method handles filtering conditions that are | ||
arrays with three elements. The first element represents the column, the second the comparison operator, and the third | ||
the value(s) to be compared. | ||
|
||
**handleArrayValueCondition** (Builder $query, array $condition): This internal method handles filtering conditions | ||
where the value is an array. It applies the whereIn or whereNotIn condition depending on the operator used. | ||
|
||
**handleSubQueryCondition** (Builder $query, string $column, Closure $subQuery): This internal method handles filtering | ||
conditions that use subqueries. The subquery is defined by a closure that is executed to construct the secondary query. | ||
|
||
**handleSubQueryConditionNot** (Builder $query, string $column, Closure $subQuery): This internal method is similar to | ||
handleSubQueryCondition but is used for conditions with the NOT IN operator. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# QueryFilters Class | ||
|
||
The QueryFilters class in the `Oscabrera\QueryFilters\Utilities` namespace provides a way to specify query filters | ||
for performing database queries in Laravel. This class allows you to define the filtering conditions for your queries. | ||
|
||
## Properties | ||
|
||
- **conditions** (array<string|int, string|bool|int|float|array<int|string, int|string|bool|float|Closure>>): Stores an | ||
associative array that defines the filtering conditions for the query. The key of the array represents the column name | ||
and the value represents the value(s) to be compared. Conditions can also be arrays specifying the operator to use. |