protected function prepareVars() { $this->areaPage = $this->page['areaPage'] = $this->property('areaPage'); $this->topicPage = $this->page['topicPage'] = $this->property('topicPage'); if ($highlight = SearchHelper::parseQuery(trim(input('search')), ['topic'], ['topic' => 'topic'])) { $this->page['highlight'] = $highlight['topic']; } }
/** * Search topics and posts. * * @param QueryBuilder $query * @param string $search * @param bool $includePosts * @return QueryBuilder */ public function scopeSearch($query, $search, $includePosts = True) { $search = trim($search); if (strlen($search)) { $parsed = Search::parseQuery($search, ['topic', 'post'], ['topic' => 'topic', 'post' => 'post', 'author' => 'author', 'by' => 'author']); // Search authors? $authors = !empty($parsed['author']) ? User::whereIn(DB::raw('LOWER(username)'), $parsed['author'])->lists('id') : []; $query->where(function ($query) use($parsed, $authors, $includePosts) { if (!empty($parsed['topic'])) { $query->searchWhere(implode(' ', $parsed['topic']), 'name'); if ($authors && empty($parsed['post'])) { $query->whereIn('author_id', $authors); } } if ($includePosts && !empty($parsed['post'])) { $query->orWhereHas('posts', function ($query) use($parsed, $authors) { $query->searchWhere(implode(' ', $parsed['post']), 'post'); if ($authors) { $query->whereIn('author_id', $authors); } }); } }); } return $query; }
/** * Search topics and posts. * * @param QueryBuilder $query * @param string $search * @return QueryBuilder */ public function scopeSearch($query, $search) { $parsed = Search::parseQuery($search, ['post'], ['post' => 'post', 'author' => 'author', 'by' => 'author']); // Search authors $authors = !empty($parsed['author']) ? User::whereIn(DB::raw('LOWER(username)'), $parsed['author'])->lists('id') : []; if ($authors) { $query->whereIn('author_id', $authors); } // Search posts if (!empty($parsed['post'])) { $query->searchWhere(implode(' ', $parsed['post']), 'post'); } return $query; }