Skip to content

Commit 485ca08

Browse files
committed
Make allowFilters feature smarter
1 parent 8b52c9c commit 485ca08

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class User extends Model
7676
}
7777
```
7878

79-
Once the trait is added, you can call the filter() method on your model statically:
79+
Once the trait is added, you can call the `filter()` method on your model statically:
8080

8181
```php
8282
use App\Models\User;
@@ -88,8 +88,8 @@ $users = User::filter([
8888
])->get(); // or ->first()
8989
```
9090

91-
* Without Filterable, filter() won’t exist on your model.
92-
* You can optionally define $allowedFilters in the model or rely on the config for auto-generated filters. If your $allowedFilters is protected, you have to define a public get method to access it. For example:
91+
* Without Filterable, `filter()` won’t exist on your model.
92+
* You can optionally define $allowedFilters in the model or rely on the config for auto-generated filters. If your `$allowedFilters` is protected, you have to define a public get method to access it. For example:
9393

9494
```php
9595
protected array $allowedFilters = ['status', 'name', 'role'];
@@ -100,12 +100,18 @@ $users = User::filter([
100100
}
101101
```
102102

103-
If you know what you are doing, you can probably just leave $allowedFilters as public property
103+
If you know what you are doing, you can probably just leave `$allowedFilters` as public property
104104

105105
```php
106106
public array $allowedFilters = ['status', 'name', 'role'];
107107
```
108108

109+
Or you can actually allow all table columns to be filtered by setting the default value in `config/query-filters.php`
110+
111+
```bash
112+
'allowed_filters' => ['*']
113+
```
114+
109115
And also supports laravel paginate and simplePaginate
110116

111117
```php
@@ -124,7 +130,7 @@ Filters also work directly from URL query parameters:
124130
```
125131
/users?status=active&role=admin&q=john&per_page=5&sort=-created_at
126132
```
127-
Which you can pass directly from your request helper or injected Illuminate\Http\Request
133+
Which you can pass directly from your request helper or injected `Illuminate\Http\Request`
128134

129135
```php
130136
use App\Models\User;
@@ -143,10 +149,10 @@ $users = User::filter($filters)->paginate(15);
143149
$users = User::filter($filters)->simplePaginate(10);
144150
$user = User::filter($filters)->first();
145151
```
146-
* ->get() → retrieves all matching records.
147-
* ->paginate() → retrieves paginated results with page links.
148-
* ->simplePaginate() → retrieves simpler pagination without total count.
149-
* ->first() → retrieves the first matching record.
152+
* `->get()` → retrieves all matching records.
153+
* `->paginate()` → retrieves paginated results with page links.
154+
* `->simplePaginate()` → retrieves simpler pagination without total count.
155+
* `->first()` → retrieves the first matching record.
150156

151157
This allows you to combine filtering with any Laravel query workflow seamlessly.
152158

config/query-filters.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,19 @@
4646

4747
/*
4848
|--------------------------------------------------------------------------
49-
| Default Allowed Filters
49+
| Allowed Filters
5050
|--------------------------------------------------------------------------
5151
|
52-
| Columns that can be filtered globally. Only listed keys are allowed.
53-
| Example usage in URL:
52+
| By default, ['*'] means all columns are allowed to be filtered.
53+
|
54+
| - To restrict filtering, define a list of allowed columns:
55+
| ['name', 'email', 'status']
5456
|
55-
| /users?status=active&role=admin
57+
| - You can also override this in individual models by
58+
| defining a public $allowedFilters = ['...'] property.
5659
|
5760
*/
58-
'allowed_filters' => [
59-
// 'status',
60-
// 'role',
61-
// 'email',
62-
// 'created_at',
63-
],
61+
'allowed_filters' => ['*'],
6462

6563
/*
6664
|--------------------------------------------------------------------------
@@ -117,7 +115,7 @@
117115
|
118116
*/
119117
'pagination' => [
120-
'per_page' => 10, // default items per page
118+
'per_page' => 10, // default items per page
121119
'max_per_page' => 100, // maximum items allowed per page
122120
],
123121

src/Concerns/HandlesAllowedFilters.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Obrainwave\LaravelQueryFilters\Concerns;
44

5+
use Illuminate\Support\Facades\Schema;
6+
57
trait HandlesAllowedFilters
68
{
79
protected function getAllowedFilters(): array
@@ -12,7 +14,19 @@ protected function getAllowedFilters(): array
1214
return $model->allowedFilters;
1315
}
1416

15-
return config('query-filters.allowed_filters', []);
17+
$allowed = config('query-filters.allowed_filters', ['*']);
18+
19+
// If wildcard used, resolve to actual columns
20+
if (in_array('*', $allowed, true) && $model) {
21+
try {
22+
return Schema::getColumnListing($model->getTable());
23+
} catch (\Throwable $e) {
24+
// Fallback to empty array if schema lookup fails
25+
return [];
26+
}
27+
}
28+
29+
return $allowed;
1630
}
1731

1832
protected function isAllowedFilter(string $key, array $allowed): bool

0 commit comments

Comments
 (0)