diff --git a/src/Controllers/ThrustHasManyController.php b/src/Controllers/ThrustHasManyController.php index 4d82aa8b..1b76a256 100644 --- a/src/Controllers/ThrustHasManyController.php +++ b/src/Controllers/ThrustHasManyController.php @@ -5,6 +5,7 @@ use Illuminate\Routing\Controller; use BadChoice\Thrust\Facades\Thrust; use BadChoice\Thrust\ChildResource; +use BadChoice\Thrust\Html\Index; class ThrustHasManyController extends Controller { @@ -24,10 +25,18 @@ public function index($resourceName, $id, $relationship) 'parent_id' => $id, 'isChild' => $resource instanceof ChildResource && $backHasManyURLParams, 'hasManyBackUrlParams' => $backHasManyURLParams, + 'searchUrl' => "/thrust/{$hasManyField->resourceName}/hasMany/{$id}/search/" // "object" => $object, // "title" => $object->{$resource->nameField}, // "children" => $object->{$relationship}, // "belongsToManyField" => $hasManyField, ]); } + + public function search($resourceName, $id, $searchText) + { + request()->merge(['search' => $searchText]); + $resource = Thrust::make($resourceName)->parentId($id); + return (new Index($resource::$searchResource ? new $resource::$searchResource: $resource))->show(); + } } diff --git a/src/Controllers/ThrustSearchController.php b/src/Controllers/ThrustSearchController.php index 71d3827a..67f80d3d 100644 --- a/src/Controllers/ThrustSearchController.php +++ b/src/Controllers/ThrustSearchController.php @@ -12,6 +12,6 @@ public function index($resourceName, $searchText) { request()->merge(['search' => $searchText]); $resource = Thrust::make($resourceName); - return (new Index($resource))->show(); + return (new Index($resource::$searchResource ? new $resource::$searchResource: $resource))->show(); } } diff --git a/src/Resource.php b/src/Resource.php index ffe81d12..cc70c271 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -43,6 +43,11 @@ abstract class Resource */ public static $search = []; + /** + * Defines the resource to search from this resource. If not defined, searches on itself + * When defined, the search is global for the resource + */ + public static $searchResource; /** * @var Defines the global gate ability for the actions to be performed, diff --git a/src/ResourceFilters/Search.php b/src/ResourceFilters/Search.php index 32b16cda..b746ee82 100644 --- a/src/ResourceFilters/Search.php +++ b/src/ResourceFilters/Search.php @@ -8,11 +8,11 @@ class Search { public static function apply($query, $searchText, $searchFields) { - $searchFields = collect($searchFields); - $firstField = $searchFields->shift(); - static::applyField($query, $firstField, $searchText); + return $query->where(function ($query) use($searchText, $searchFields){ + $searchFields = collect($searchFields); + $firstField = $searchFields->shift(); + static::applyField($query, $firstField, $searchText); - return $query->orWhere(function ($query) use($searchText, $searchFields){ return $searchFields->reduce(function($query, $searchField) use($searchText) { return static::applyFieldSimple($query, $searchField, $searchText); }, $query); diff --git a/src/resources/views/components/searchScript.blade.php b/src/resources/views/components/searchScript.blade.php index db1c1181..ce3fa70e 100644 --- a/src/resources/views/components/searchScript.blade.php +++ b/src/resources/views/components/searchScript.blade.php @@ -1,5 +1,5 @@