Skip to content

Commit dbeeaa5

Browse files
author
oooiik
committed
update methods
1 parent 3f93b8f commit dbeeaa5

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Laravel Query Filter
2+
3+
In Laravel, it is convenient to write the query filter related to the model in a separate class!
4+
5+
### Installation
6+
7+
```
8+
composer require oooiik/laravel-query-filter
9+
```
10+
11+
### Usage:
12+
13+
for single use
14+
```php
15+
User::filter($validated)->get();
16+
```
17+
or create a filter
18+
```php
19+
$userFilter = User::createFilter(UserFilter::class);
20+
```
21+
22+
get a query using a filter
23+
```php
24+
$userFilter->apply($validated)->query();
25+
```
26+
write filter on filter and get a query
27+
```php
28+
$userFilter->apply($validated);
29+
$userFilter->apply($validated_2)->query();
30+
```
31+
filter cleaning and reuse
32+
```php
33+
$userFilter->resetApply($validated_3)->query();
34+
```
35+
36+
In order to use a filter you have to create a new one by the command that is provided by the package:
37+
38+
```
39+
php artisan make:filter UserFilter
40+
```
41+
This command will create a directory `Filters` and `UserFilter` class inside. To use the filter method of `User` model use the `Filterable` trait:
42+
43+
```php
44+
<?php
45+
46+
namespace App\Models;
47+
48+
use Oooiik\LaravelQueryFilter\Traits\Model\Filterable;
49+
50+
class User extends Model
51+
{
52+
use Filterable;
53+
54+
```
55+
And set the `defaultFilter` of a model by adding:
56+
57+
```php
58+
protected $defaultFilter = UserFilter::class;
59+
```
60+
You can create a custom query by creating a new function in the `Filter` class, for example filtering books by publishing date:
61+
```php
62+
public function username($username)
63+
{
64+
$this->builder->where('username', $username);
65+
}
66+
// $validated = ['username' => 'name']
67+
```
68+
or filter by relationship:
69+
```php
70+
public function role($role)
71+
{
72+
$this->builder->whereHas('role', function($query) use ($role) {
73+
$query->where('title', $role);
74+
})
75+
}
76+
// $validated = ['role' => 'admin']
77+

src/Traits/Model/Filterable.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
use Symfony\Component\ErrorHandler\Error\ClassNotFoundError;
88

99
/**
10-
* @method static Builder filter(string $filter, array $validated = [])
10+
* @method static Builder filter(array $validated = [])
11+
* @property string $defaultFilter;
1112
*/
1213
trait Filterable
1314
{
14-
public function scopeFilter(Builder $query, $filter, array $validated = [])
15+
// protected $defaultFilter;
16+
17+
public function scopeFilter(Builder $query, array $validated = [])
1518
{
16-
if (!class_exists($filter)) {
19+
if (!class_exists($this->defaultFilter)) {
1720
throw new ClassNotFoundError('Class not found', 500);
1821
}
19-
if (!is_subclass_of($filter, QueryFilter::class)) {
22+
if (!is_subclass_of($this->defaultFilter, QueryFilter::class)) {
2023
throw new ClassNotFoundError('It is not a successor class of Filter', 500);
2124
}
22-
return $filter::builder($query)->apply($validated)->query();
25+
return $this->defaultFilter::builder($query)->apply($validated)->query();
2326
}
2427

2528
/**

0 commit comments

Comments
 (0)