コード例 #1
0
ファイル: Topic.php プロジェクト: anqqa/oc-forum-plugin
 /**
  * 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;
 }