+ return $this->permissionService->enforceEntityRestrictions($entity, $entityQuery, $action);
+ }
+
+ /**
+ * For the given search query, apply the queries for handling the regular search terms.
+ */
+ protected function applyTermSearch(EloquentBuilder $entityQuery, array $terms, Entity $entity): void
+ {
+ if (count($terms) === 0) {
+ return;
+ }
+
+ $subQuery = DB::table('search_terms')->select([
+ 'entity_id',
+ 'entity_type',
+ DB::raw('SUM(score) as score'),
+ ]);
+
+ $subQuery->where('entity_type', '=', $entity->getMorphClass());
+ $subQuery->where(function (Builder $query) use ($terms) {
+ foreach ($terms as $inputTerm) {
+ $query->orWhere('term', 'like', $inputTerm . '%');
+ }
+ })->groupBy('entity_type', 'entity_id');
+ $entityQuery->join(DB::raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) {
+ $join->on('id', '=', 'entity_id');
+ })->addSelect($entity->getTable() . '.*')
+ ->selectRaw('s.score')
+ ->orderBy('score', 'desc');
+ $entityQuery->mergeBindings($subQuery);