diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index 11295ea70..09abec637 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -361,25 +361,6 @@ class Lists extends WidgetBase $joins[] = $column->relation; } - /* - * Include any relation constraints - */ - if ($joins) { - foreach (array_unique($joins) as $join) { - /* - * Apply a supplied search term for relation columns and - * constrain the query only if there is something to search for - */ - $columnsToSearch = array_get($relationSearchable, $join, []); - - if (count($columnsToSearch) > 0) { - $query->whereHas($join, function ($_query) use ($columnsToSearch) { - $_query->searchWhere($this->searchTerm, $columnsToSearch); - }); - } - } - } - /* * Add eager loads to the query */ @@ -387,6 +368,39 @@ class Lists extends WidgetBase $query->with(array_unique($withs)); } + /* + * Apply search term + */ + $query->where(function ($innerQuery) use ($primarySearchable, $relationSearchable, $joins) { + + /* + * Search primary columns + */ + if (count($primarySearchable) > 0) { + $innerQuery->orSearchWhere($this->searchTerm, $primarySearchable); + } + + /* + * Search relation columns + */ + if ($joins) { + foreach (array_unique($joins) as $join) { + /* + * Apply a supplied search term for relation columns and + * constrain the query only if there is something to search for + */ + $columnsToSearch = array_get($relationSearchable, $join, []); + + if (count($columnsToSearch) > 0) { + $innerQuery->orWhereHas($join, function ($_query) use ($columnsToSearch) { + $_query->searchWhere($this->searchTerm, $columnsToSearch); + }); + } + } + } + + }); + /* * Custom select queries */ @@ -428,15 +442,6 @@ class Lists extends WidgetBase } } - /* - * Apply a supplied search term for primary columns - */ - if (count($primarySearchable) > 0) { - $query->where(function ($innerQuery) use ($primarySearchable) { - $innerQuery->searchWhere($this->searchTerm, $primarySearchable); - }); - } - /* * Apply sorting */