From 84a1932d513dc07239f615a35b84904956fffbff Mon Sep 17 00:00:00 2001 From: Skullbock Date: Fri, 6 Aug 2021 08:49:48 +0200 Subject: [PATCH] allow resource index query to be customized with a named scope --- README.md | 10 ++++++++++ .../Controllers/ResourceListingController.php | 16 ++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 964ae868..4b0d2715 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,16 @@ You may also customise certain aspects of the CP Listing, like the icon used in ], ``` +You can also customize the way the CP search works in the Listing page, by adding a scope named `runwaySearch` to your model. +If there is no scope set, Runway will do a simple `orWhere` query on your model's fields. + +```php +public function scopeRunwaySearch($query, $search) +{ + $query->where('some_field', $search); +} +``` + ## Usage ### Control Panel diff --git a/src/Http/Controllers/ResourceListingController.php b/src/Http/Controllers/ResourceListingController.php index dae8b572..786e7a17 100644 --- a/src/Http/Controllers/ResourceListingController.php +++ b/src/Http/Controllers/ResourceListingController.php @@ -26,13 +26,17 @@ public function index(FilteredRequest $request, $resourceHandle) ->orderBy($sortField, $sortDirection); if ($searchQuery = $request->input('search')) { - $query->where(function ($query) use ($searchQuery, $blueprint) { - $wildcard = '%'.$searchQuery.'%'; + if ($query->hasNamedScope('runwaySearch')) { + $query->runwaySearch($searchQuery); + } else { + $query->where(function ($query) use ($searchQuery, $blueprint) { + $wildcard = '%' . $searchQuery . '%'; - foreach ($blueprint->fields()->items()->toArray() as $field) { - $query->orWhere($field['handle'], 'LIKE', $wildcard); - } - }); + foreach ($blueprint->fields()->items()->toArray() as $field) { + $query->orWhere($field['handle'], 'LIKE', $wildcard); + } + }); + } } $results = $query->paginate($request->input('perPage', config('statamic.cp.pagination_size')));