Skip to content

Commit f5bbf1e

Browse files
committed
fixed missed hasNamedScope function on laravel < 7
1 parent 0dbf59e commit f5bbf1e

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
## Laravel Advanced Filter
1+
# Laravel Advanced Filter
22
This package allows you to filter on laravel models
33

44
You can choose fields to filtering and customize its data-types, aliases and excepted operators,
55
you can add/customize your request format, and you add new operators or overwrite the existed operators
66

77

8-
### Installation
8+
## Installation
99
You can install the package via composer:
1010
```
1111
composer require asemalalami/laravel-advanced-filter
@@ -21,7 +21,7 @@ php artisan vendor:publish --provider="AsemAlalami\LaravelAdvancedFilter\Advance
2121
These default config file that will be published:
2222
[Config File](https://github.com/AsemAlalami/Laravel-Advanced-Filter/blob/master/config/advanced_filter.php)
2323

24-
### Usage
24+
## Usage
2525
- use `HasFilter` trait in the model
2626
- add fields in the implementation of the abstract function `setupFilter`
2727
```php
@@ -72,7 +72,7 @@ class Order extends Model
7272
}
7373
```
7474

75-
### Query Format
75+
## Query Format
7676
Query format is the shape that you want to send your query(filters) in the request.
7777
the package support 3 formats, and you can create a new format.
7878
- `json` (default): the filters will send as json in the request
@@ -91,12 +91,12 @@ the package support 3 formats, and you can create a new format.
9191
```
9292
> set the default query format in the config file `query_format` attribute
9393

94-
##### Create a new query format:
94+
#### Create a new query format:
9595
- create a new class and extends it from `QueryFormat`: `class MyFormat extends QueryFormat`
9696
- implement the abstract function `format` that returns `FilterRequest` object
9797
- add the class to the config file in `custom_query_format` attribute: `'custom_query_format' => MyFormat::class,`
9898

99-
### Fields
99+
## Fields
100100
Normal Field options:
101101
- field name is the column name
102102
- alias is the key that you want to send in the request
@@ -156,11 +156,11 @@ You can add fields to a model by using 4 functions:
156156
$this->addCustomField('line_subtotal', '(`price` + `quantity`)', 'orderLines'); // inside "orderLines" relation
157157
```
158158

159-
### Conjunction
159+
## Conjunction
160160
Currently, the package support one conjunction between all fields
161161
`and` | `or`, default conjunction attribute in the config file `default_conjunction`
162162

163-
### Operators
163+
## Operators
164164
The package has many operators, you can create new operators,
165165
and you can customize the operators aliases that you want to send in the request
166166
- Equals (`=`, `equals`)
@@ -179,17 +179,17 @@ and you can customize the operators aliases that you want to send in the request
179179
- NotEndsWith (`!$`, `notEndsWith`)
180180
- Between (`><`, `between`)
181181

182-
##### Create a new Operator:
182+
#### Create a new Operator:
183183
- create a new class and extends it from `Operator`: `class MyOperator extends Operator`
184184
- implement the abstract function `apply` and `getSqlOperator` (used as a default sql operator for count and custom field)
185185
- add the class in the config file in `custom_operators` attribute: `'custom_operators' => [MyOperator::class => ['my-op', '*']],`
186186

187-
### Data Types:
187+
## Data Types:
188188
- boolean
189189
- date
190190
- datetime
191191
- numeric
192192
- string
193193

194-
### Config
194+
## Config
195195
[Config File](https://github.com/AsemAlalami/Laravel-Advanced-Filter/blob/master/config/advanced_filter.php)

src/HasFilter.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ private function apply(Builder $builder, $request)
6262
// apply filter inside relation if the field from relation
6363
if ($field->isFromRelation()) {
6464
// apply on custom scope if the relation has scope
65-
if ($builder->hasNamedScope($field->getScopeRelationFunctionName())) {
65+
if ($this->modelHasScope($builder, $field->getScopeRelationFunctionName())) {
6666
$builder = $builder->{$field->getScopeRelationFunctionName()}($field, $operator, $value, $conjunction);
6767
} else {
6868
$builder = $builder->has($field->getRelation(), '>=', 1, $conjunction,
6969
function (Builder $builder) use ($field, $value, $operator) {
7070
return $this->filterField($builder, $field, $operator, $value);
7171
}
7272
);
73-
// $builder->whereDoesntHave()
7473
}
7574
} else {
7675
// apply on field
@@ -98,13 +97,25 @@ function (Builder $builder) use ($field, $value, $operator) {
9897
private function filterField(Builder $builder, Field $field, $operator, $value, $conjunction = 'and')
9998
{
10099
// apply on custom scope if the field has scope
101-
if ($builder->hasNamedScope($field->getScopeFunctionName())) {
100+
if ($this->modelHasScope($builder, $field->getScopeFunctionName())) {
102101
return $builder->{$field->getScopeFunctionName()}($field, $operator, $value, $conjunction);
103102
}
104103

105104
return $builder->{Operator::getFunction($operator)}($field, $value, $conjunction);
106105
}
107106

107+
/**
108+
* Determine if the given model has a scope.
109+
*
110+
* @param Builder $builder
111+
* @param $scopeName
112+
* @return bool
113+
*/
114+
private function modelHasScope(Builder $builder, $scopeName)
115+
{
116+
return $builder->getModel() && method_exists($builder->getModel(), 'scope' . ucfirst($scopeName));
117+
}
118+
108119
public function initializeHasFilter()
109120
{
110121
$this->setupFilter();

0 commit comments

Comments
 (0)