Skip to content

Commit 8b52c9c

Browse files
committed
Fixed global search key bug
1 parent e9ca6b0 commit 8b52c9c

File tree

3 files changed

+71
-31
lines changed

3 files changed

+71
-31
lines changed

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ composer require obrainwave/laravel-query-filters
1313
Publish the config (optional):
1414

1515
```bash
16-
php artisan vendor:publish --provider="Obrainwave\LaravelQueryFilters\LaravelQueryFiltersServiceProvider" --tag="query-filters-config"
16+
php artisan vendor:publish --tag=query-filters-config
1717
```
1818

1919
---
@@ -27,12 +27,7 @@ return [
2727

2828
'global_key' => 'q', // Query parameter key for global search
2929

30-
'allowed_filters' => [
31-
'status',
32-
'role',
33-
'email',
34-
'created_at',
35-
],
30+
'allowed_filters' => ['name', 'role'],
3631

3732
'default_match' => 'exact', // exact, like, strict
3833

@@ -44,7 +39,7 @@ return [
4439
],
4540

4641
'pagination' => [
47-
'per_page' => 15,
42+
'per_page' => 10,
4843
'max_per_page' => 100,
4944
],
5045

config/query-filters.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,29 @@
2020
| You can also override per model via $globalSearchKey property.
2121
|
2222
*/
23-
'global_key' => 'q',
23+
'global_key' => 'q',
24+
25+
/*
26+
|--------------------------------------------------------------------------
27+
| Strict Global Search
28+
|--------------------------------------------------------------------------
29+
|
30+
| This option controls how the package handles global search columns.
31+
|
32+
| - When set to false (default), the filter will silently ignore any
33+
| invalid or non-existent columns. This is useful when you want the
34+
| search to "just work" without throwing errors.
35+
|
36+
| - When set to true, the filter will throw an exception if you define
37+
| a global search column that does not exist on the model’s table.
38+
| (Fallback columns like name, title, body, etc. are automatically
39+
| validated against the schema and will never throw errors.)
40+
|
41+
| This gives you the flexibility to decide between convenience and
42+
| strict validation depending on your project’s needs.
43+
|
44+
*/
45+
'strict_global_search' => false,
2446

2547
/*
2648
|--------------------------------------------------------------------------
@@ -33,11 +55,11 @@
3355
| /users?status=active&role=admin
3456
|
3557
*/
36-
'allowed_filters' => [
37-
'status',
38-
'role',
39-
'email',
40-
'created_at',
58+
'allowed_filters' => [
59+
// 'status',
60+
// 'role',
61+
// 'email',
62+
// 'created_at',
4163
],
4264

4365
/*
@@ -55,7 +77,7 @@
5577
| /users?name=John&mode=like => partial match if mode is changed
5678
|
5779
*/
58-
'default_match' => 'exact',
80+
'default_match' => 'exact',
5981

6082
/*
6183
|--------------------------------------------------------------------------
@@ -75,7 +97,7 @@
7597
| 'user.*' => 'like' // wildcard on all user columns
7698
|
7799
*/
78-
'filter_modes' => [
100+
'filter_modes' => [
79101
// 'email' => 'strict',
80102
// 'name' => 'like',
81103
// 'user.*' => 'like',
@@ -94,8 +116,8 @@
94116
| Config fallback if none specified
95117
|
96118
*/
97-
'pagination' => [
98-
'per_page' => 1, // default items per page
119+
'pagination' => [
120+
'per_page' => 10, // default items per page
99121
'max_per_page' => 100, // maximum items allowed per page
100122
],
101123

@@ -113,9 +135,9 @@
113135
| 'allowed_columns' => ['name', 'created_at']
114136
|
115137
*/
116-
'sorting' => [
138+
'sorting' => [
117139
'allowed_columns' => [], // empty = all allowed
118-
'default' => null, // e.g., 'created_at' or '-created_at'
140+
'default' => null, // e.g., 'created_at' or '-created_at'
119141
],
120142

121143
/*
@@ -135,7 +157,7 @@
135157
| Disable by setting 'enabled' => false
136158
|
137159
*/
138-
'operators' => [
160+
'operators' => [
139161
'enabled' => true,
140162
],
141163

src/Concerns/HandlesGlobalSearch.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Obrainwave\LaravelQueryFilters\Concerns;
43

54
trait HandlesGlobalSearch
@@ -21,23 +20,47 @@ protected function getGlobalSearchKey(): string
2120

2221
protected function applyGlobalSearch(string $term): void
2322
{
24-
$term = strtolower($term);
25-
$model = $this->builder->getModel();
23+
$term = strtolower($term);
24+
$model = $this->builder->getModel();
25+
$allColumns = \Schema::getColumnListing($model->getTable());
2626

2727
if ($model && method_exists($model, 'getGlobalSearchColumns')) {
2828
$columns = $model->getGlobalSearchColumns();
29-
} elseif ($model && property_exists($model, 'globalSearchColumns') &&
29+
} elseif (
30+
$model &&
31+
property_exists($model, 'globalSearchColumns') &&
3032
is_array($model->globalSearchColumns) &&
31-
! empty($model->globalSearchColumns)) {
33+
! empty($model->globalSearchColumns)
34+
) {
3235
$columns = $model->globalSearchColumns;
3336
} else {
34-
$columns = ['name', 'title', 'content', 'body', 'message', 'subject'];
37+
$columns = array_intersect(
38+
['name', 'title', 'content', 'body', 'message', 'subject'],
39+
$allColumns
40+
);
41+
3542
}
3643

37-
$this->builder->where(function ($query) use ($term, $columns) {
38-
foreach ($columns as $column) {
39-
$query->orWhereRaw("LOWER({$column}) LIKE ?", ["%{$term}%"]);
44+
$strict = config('queryfilters.strict_global_search', false);
45+
46+
$validColumns = [];
47+
foreach ($columns as $column) {
48+
if (\Schema::hasColumn($model->getTable(), $column)) {
49+
$validColumns[] = $column;
50+
} elseif ($strict) {
51+
throw new \InvalidArgumentException(
52+
"Global search column '{$column}' does not exist on table '{$model->getTable()}'"
53+
);
4054
}
41-
});
55+
}
56+
57+
if (! empty($validColumns)) {
58+
$this->builder->where(function ($query) use ($term, $validColumns) {
59+
foreach ($validColumns as $column) {
60+
$query->orWhereRaw("LOWER({$column}) LIKE ?", ["%{$term}%"]);
61+
}
62+
});
63+
}
4264
}
65+
4366
}

0 commit comments

Comments
 (0)