Skip to content

Commit 3e12e0b

Browse files
committed
Initial commit
0 parents  commit 3e12e0b

31 files changed

+1790
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor
2+
/.idea
3+
/.vscode
4+
composer.lock
5+
.phpunit.result.cache

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Cuthbert Mirambo <miracuthbert@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Laravel Eloquent Filters
2+
3+
A package for Laravel that can be used to filter a model's records.
4+
5+
## How does it work?
6+
7+
Simply the package checks the request query for keys that match the corresponding filter keys set for the model then builds the query.
8+
9+
Each model has a corresponding `filters` file where it's filters can be registered and mapped.
10+
11+
Each registered `filter` is a unique file that contains the necessary functionality to build a query.
12+
13+
This means that for models that have a `title` column, they can share the call the same filter file hence reducing code duplication.
14+
15+
## Installation
16+
17+
Use composer to install the package:
18+
19+
```
20+
composer require miracuthbert/laravel-eloquent-filters
21+
```
22+
23+
## Setup
24+
25+
The package takes advantage of Laravel Auto-Discovery, so it doesn't require you to manually add the ServiceProvider.
26+
27+
If you don't use auto-discovery, add the ServiceProvider to the providers array in `config/app.php`
28+
29+
```php
30+
Miracuthbert\Filters\EloquentFiltersServiceProvider::class
31+
```
32+
33+
If you want to publish the `config` file use the commands below in your console
34+
35+
### Publish Config
36+
37+
```
38+
php artisan vendor:publish --provider=Miracuthbert\Filters\EloquentFiltersServiceProvider --tag=filters-config
39+
```
40+
41+
## Usage
42+
43+
## Setting up a model
44+
45+
A filter for a model generally extends the `Miracuthbert\Filters\FiltersAbstract`.
46+
47+
It contains a list of filters that will should be applied to a the model and a map of `key/value` pair of filters list.
48+
49+
To create one:
50+
51+
First, create a model with `php artisan make:model` command.
52+
53+
Then create a filter file for the model using:
54+
55+
```
56+
php artisan filter:model {name}
57+
58+
// example
59+
php artisan filter:make UserFilters
60+
61+
// within specific namespace
62+
php artisan filter:make Users\\UserFilters
63+
```
64+
65+
Switch `name` with the model name and preferably add suffix `Filters` to indicate it is a model filter, eg. `UserFilters`
66+
67+
> Copy and add the block of code printed out in the console, to the related model. Do not forget to pull in the required namespaces.
68+
69+
### Using Filter in Controller
70+
71+
After setup above in any controller that you call the model instance, call the `filter` scope passing an instance of the `Illuminate\Http\Request'.
72+
73+
```php
74+
$users = User::filter($request)->get();
75+
```
76+
77+
In case you have disabled appending of the filters query to a paginator, you could do it manually by using the `filters_query` helper:
78+
79+
```php
80+
$users->appends(filters_query());
81+
```
82+
83+
```blade
84+
// in blade view
85+
{{ $users->appends(filters_query())->links() }}
86+
```
87+
88+
## Creating a Filter
89+
90+
All created filters by default will be placed within, `App\Filters` directory (namespace).
91+
92+
To create a filter use:
93+
94+
```
95+
php artisan filter:make {name}
96+
97+
// example
98+
php artisan filter:make NameFilter
99+
100+
// within specific namespace
101+
php artisan filter:make Users\\NameFilter
102+
```
103+
104+
Switch `name` with the name of the filter, eg. `NameFilter` (normal filter), `CreatedOrder` (ordering filter)
105+
106+
> When creating a filter, it is good to add `Filter` or `Order` to the name for ease of use.
107+
108+
You can then open the filter file and add your custom query functionality. See the `filter:make` command options for some preset templates.
109+
110+
> Filters are basically blocks of code that extend `Illuminate\Database\Eloquent\Builder`, so you are not limited to the preset templates.
111+
112+
You can pass, the following options to the `filter:make` command, to make use of some of the common filter templates:
113+
114+
- `column`, Indicates the column the filter should be implemented on
115+
- `bool`, Indicates if generated filter should be a boolean filter class
116+
- `null`, Indicates if generated filter should be a null check filter class
117+
- `order`, Indicates if generated filter should be an order filter class
118+
- `relation`, Generates a filter class for the given relationship
119+
120+
## Registering Filters to Model Filters
121+
122+
After creating a filter, to use it open a model filter and register a `key/value` pair under the `$filters` field.
123+
124+
```php
125+
/**
126+
* A list of filters.
127+
*
128+
* @var array
129+
*/
130+
protected $filters = [
131+
'name' => NameFilter::class,
132+
'email' => EmailFilter::class,
133+
'created' => CreatedOrder::class,
134+
];
135+
```
136+
137+
## Setting Default Filters
138+
139+
In a model filter, you can register a `key/value` pair under the `$defaultFilters` field, for filters you want to be applied by default.
140+
141+
> `key` should be the same as the one registered on the `$filters` field, `value` should be an actual database value.
142+
143+
_Note_: Default filters should only be used for filters with `fixed` or `unchanging` values, eg. `true`, `false`
144+
145+
```php
146+
/**
147+
* A list of default filters.
148+
*
149+
* @var array
150+
*/
151+
protected $defaultFilters = [
152+
'created' => 'desc',
153+
];
154+
```
155+
156+
> You can override default filters in the constructor of a model filter, by adding different checks.
157+
158+
```php
159+
// constructor
160+
161+
if ($request->hasAny('cancelled', 'completed')) {
162+
$this->defaultFilters = [
163+
'upcoming' => 'false'
164+
];
165+
}
166+
```
167+
168+
## Console Commands
169+
170+
There available commands within the package:
171+
172+
- `filter:model`: Create a new filter class for a model
173+
- `filter:make`: Creates a new Eloquent filter class
174+
175+
> Pass `--help` option to the commands to get more details on using them
176+
177+
## Security Vulnerabilities
178+
179+
If you discover a security vulnerability, please send an e-mail to Cuthbert Mirambo via [miracuthbert@gmail.com](mailto:miracuthbert@gmail.com). All security vulnerabilities will be promptly addressed.
180+
181+
## Credits
182+
183+
- [Cuthbert Mirambo](https://github.com/miracuthbert)
184+
185+
## License
186+
187+
The project is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

composer.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "miracuthbert/laravel-eloquent-filters",
3+
"description": "An eloquent filters package for Laravel 5.8 and up",
4+
"keywords": [
5+
"miracuthbert",
6+
"laravel",
7+
"filters",
8+
"eloquent-filters"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Cuthbert Mirambo",
14+
"email": "miracuthbert@gmail.com"
15+
}
16+
],
17+
"require": {
18+
"php": "^7.1.3",
19+
"illuminate/console": "5.8.*|^6.0",
20+
"illuminate/database": "5.8.*|^6.0",
21+
"illuminate/http": "5.8.*|^6.0",
22+
"illuminate/support": "5.8.*|^6.0"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^8.0",
26+
"orchestra/testbench": "^3.8"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Miracuthbert\\Filters\\": "src/"
31+
},
32+
"files": [
33+
"src/Helpers/helpers.php"
34+
]
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"Miracuthbert\\Filters\\Tests\\": "tests/"
39+
}
40+
},
41+
"minimum-stability": "dev",
42+
"prefer-stable": true,
43+
"extra": {
44+
"laravel": {
45+
"providers": [
46+
"Miracuthbert\\Filters\\EloquentFiltersServiceProvider"
47+
],
48+
"aliases": {
49+
"Filters": "Miracuthbert\\Filters\\Facades\\Filters"
50+
}
51+
}
52+
}
53+
}

config/laravel-filters.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
*
7+
* Filters Namespace.
8+
*
9+
* The namespace under 'app' folder to place created filters.
10+
*
11+
*/
12+
'namespace' => 'Filters',
13+
14+
/*
15+
*
16+
* Pagination.
17+
*
18+
* Filters pagination.
19+
*
20+
*/
21+
'pagination' => [
22+
23+
/*
24+
*
25+
* Append Filter Query.
26+
*
27+
* Appends the filters in current request to paginator.
28+
*
29+
*/
30+
'append_filter_query' => true,
31+
],
32+
33+
];

phpunit.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Laravel Eloquent Filters test suite">
13+
<directory suffix="Test.php">./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

0 commit comments

Comments
 (0)