Skip to content

Commit 15b4e16

Browse files
author
game5413
committed
initial commit
1 parent ed6bb83 commit 15b4e16

File tree

6 files changed

+433
-1
lines changed

6 files changed

+433
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.phpunit.result.cache
3+
vendor
4+
composer.lock

README.md

+225-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,226 @@
1-
# laravel-restful-filter
1+
# Laravel RESTful Filter
22
A simple way to filter your query on RESTful API
3+
4+
# Compatibility
5+
| Framework | Version |
6+
| :------------- | :----------: |
7+
| Laravel | `5.*`, `6.*`, `7.*`, `8.*` |
8+
| Lumen | `5.*`, `6.*`, `7.*`, `8.*` |
9+
10+
# Limitation
11+
- Currently, only working on SQL type Database (already tested on MySQL, PostgreSQL, SQL Server)
12+
- Probably SQL query not optimized (already checked on this but i think it doesn't have, the result was fast. In the end, it's your choise)
13+
- Sorting through relation not available
14+
- Filter `Between` and `In` not available
15+
- Can't add custom logic
16+
17+
# Available Search Operators
18+
- Less Than
19+
- Less Than Equal
20+
- Greater Than
21+
- Greater Than Equal
22+
- Equal
23+
- LIKE
24+
- NOT
25+
26+
# Installation
27+
Install from composer
28+
```bash
29+
composer install kemodev/laravel-restful-filter
30+
```
31+
Use Package on your Model
32+
```php
33+
<?php
34+
35+
namespace App;
36+
37+
use Kemodev\RestfulFilter\Filterable;
38+
use Illuminate\Database\Eloquent\Model;
39+
40+
class User extends Model
41+
{
42+
use Filterable;
43+
44+
//rest of your code on model
45+
}
46+
```
47+
48+
# Get Started
49+
First, you need to define what field that able to filter with your API
50+
## 1. Filter
51+
There are 3 kind available method for filter:
52+
- Basic
53+
```php
54+
<?php
55+
56+
namespace App;
57+
58+
use Kemodev\RestfulFilter\Filterable;
59+
use Illuminate\Database\Eloquent\Model;
60+
61+
class User extends Model
62+
{
63+
use Filterable;
64+
65+
protected $filterableColumns = [
66+
'email' => 'email'
67+
];
68+
//rest of your code on model
69+
}
70+
```
71+
- Relation
72+
```php
73+
<?php
74+
75+
namespace App;
76+
77+
use Kemodev\RestfulFilter\Filterable;
78+
use Illuminate\Database\Eloquent\Model;
79+
80+
class User extends Model
81+
{
82+
use Filterable;
83+
84+
protected $filterableColumns = [
85+
'email' => 'email',
86+
'role_name' => 'role.name'
87+
];
88+
89+
public function role()
90+
{
91+
$this->belongsTo(Role::class);
92+
}
93+
//rest of your code on model
94+
}
95+
```
96+
- All of Kind
97+
```php
98+
<?php
99+
100+
namespace App;
101+
102+
use Kemodev\RestfulFilter\Filterable;
103+
use Illuminate\Database\Eloquent\Model;
104+
105+
class User extends Model
106+
{
107+
use Filterable;
108+
109+
protected $filterableColumns = [
110+
'email' => 'email',
111+
'search' => 'first_name,last_name,role.name'
112+
];
113+
114+
public function role()
115+
{
116+
$this->belongsTo(Role::class);
117+
}
118+
//rest of your code on model
119+
}
120+
```
121+
This method usually used when using one field filter like on default [datatables](https://datatables.net)
122+
123+
## 2. Sorting
124+
```php
125+
<?php
126+
127+
namespace App;
128+
129+
use Kemodev\RestfulFilter\Filterable;
130+
use Illuminate\Database\Eloquent\Model;
131+
132+
class User extends Model
133+
{
134+
use Filterable;
135+
136+
protected $sortableColumns = [
137+
'email' => 'email'
138+
];
139+
140+
//rest of your code on model
141+
}
142+
```
143+
144+
**If you notice, that variables was associate array with key indicate the query param on URL and value was the column name.**
145+
146+
## 3. Usage
147+
The most basic one, you can use like these
148+
```php
149+
<?php
150+
151+
namespace App\Http\Controllers;
152+
153+
use App\Users;
154+
use Illuminate\Http\Request;
155+
156+
class UserController extends Controller
157+
{
158+
public function getAll(Request $request)
159+
{
160+
$filter = $request->except('sort');
161+
$sort = $request->get('sort');
162+
163+
$data = User::searchable($filter)
164+
->sortable($sort)
165+
->get();
166+
}
167+
}
168+
```
169+
with this code, it will assume that you produce url like <br>
170+
[https://your-domain.dev?name=febryan&[email protected]&sort=name_asc](https://your-domain.dev?name=febryan&[email protected]&sort=name_asc)
171+
172+
But the `$filter` variables take scope to wide, because you may have another query params that not related for filter your data. The best practice that i assume was write your code like these
173+
174+
```php
175+
<?php
176+
177+
namespace App\Http\Controllers;
178+
179+
use App\Users;
180+
use Illuminate\Http\Request;
181+
182+
class UserController extends Controller
183+
{
184+
public function getAll(Request $request)
185+
{
186+
$filter = $request->get('filters');
187+
$sort = $request->get('sort');
188+
189+
$data = User::searchable($filter)
190+
->sortable($sort)
191+
->get();
192+
}
193+
}
194+
```
195+
with this code, it will assume that you produce url like <br>
196+
[https://your-domain.dev?filters[name]=febryan&filters[email]=[email protected]&sort=name_asc&signature=123qweasdzxc](https://your-domain.dev?filters[name]=febryan&filters[email][email protected]&sort=name_asc&signature=123qweasdzxc)
197+
198+
For using available search operator on filter, you can input your query params like these:
199+
- Less Than: `filters[age]=lt:12`
200+
- Less Than Equal: `filters[age]=lte:12`
201+
- Greater Than: `filters[age]=gt:12`
202+
- Greater Than Equal: `filters[age]=gte:12`
203+
- Equal: `filters[name]=John Doe`
204+
- LIKE: `filters[name]=like:john`
205+
- NOT: `filters[name]=not:John Doe`
206+
207+
For change direction on sorting, you can input your query params like these:
208+
- Ascending: `sort=name_asc`
209+
- Descending: `sort=name_desc`
210+
211+
Or you want declare multiple sorting, just type your query params like these:
212+
`sort=name_asc,email_desc`
213+
214+
# Contributing
215+
Any contributions welcome! but if you are busy to contribute or you don't know how to contribute because seeing my ugly code, you can do thing on below to declare your contributions.
216+
## Stars the project
217+
If you find this package useful and you want to encourage me to maintain and work on it, Just press the star button to declare your willing.
218+
219+
## Reward me with a cup of tea 🍵
220+
Send me as much as a cup of tea worth in your country, so I'll have the energy to maintain this package.
221+
222+
<div style="position: relative;">
223+
<a href="https://paypal.me/febryanph?locale.x=id_ID" style="position: absolute; left: -10px; top: -10px;">
224+
<img src="https://raw.githubusercontent.com/stefan-niedermann/paypal-donate-button/master/paypal-donate-button.png" width="150"/>
225+
</a>
226+
</div>

composer.json

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "kemodev/laravel-restful-filter",
3+
"description": "A simple way to filter your query on RESTful API",
4+
"keywords": [
5+
"laravel",
6+
"eloquent",
7+
"model",
8+
"restful",
9+
"search",
10+
"query",
11+
"filter"
12+
],
13+
"homepage": "https://github.com/game5413/laravel-restful-filter",
14+
"type": "library",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "M Febryan",
19+
"email": "[email protected]",
20+
"homepage": "https://kemodev.my.id",
21+
"role": "Developer"
22+
}
23+
],
24+
"minimum-stability": "dev",
25+
"prefer-stable": true,
26+
"autoload": {
27+
"psr-4": {
28+
"Kemodev\\RestfulFilter\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"classmap": [
33+
"src/"
34+
]
35+
},
36+
"require": {
37+
"php": "^7.0|^8.0"
38+
},
39+
"require-dev": {
40+
"phpunit/phpunit": "^9.5",
41+
"mockery/mockery": "^1.4"
42+
},
43+
"scripts": {
44+
"test": "phpunit",
45+
"test-coverage": "phpunit --coverage-html coverage"
46+
}
47+
}

src/Exceptions/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)