【发布时间】:2019-02-01 07:39:14
【问题描述】:
在Models\Article::class 中的搜索方法中,即使在与类别和标签的多重关系中,我也能够获得所有结果,但我一直试图解决的问题是我只想要已发表文章的结果——我的 db 列是 bool 'active'。
我通过切换->where('active', 1) 子句尝试了不同的方法,但结果是相同的。它带来了所有的活动和非活动。
我有 8 篇文章正在开发中,但只有 7 篇活动 = 1。当我执行搜索时,响应发送所有 8 篇文章,忽略 where('active', 1) 子句。可能我在以下方法中遗漏或弄乱了一些东西:
// Method in the Article Model class
public function getSearchResults($query)
{
return $this->where('active', 1)->where('start_publishing', '<=', Carbon::now())
->where('external_reference', 'LIKE', '%' . $query . '%')
->orWhere('byline', 'LIKE', '%' . $query . '%')
->orWhere('published_by', 'LIKE', '%' . $query . '%')
->orWhere('title', 'LIKE', '%' . $query . '%')
->orWhere('subhead', 'LIKE', '%' . $query . '%')
->orWhere('lead_story', 'LIKE', '%' . $query . '%')
->orWhere('content', 'LIKE', '%' . $query . '%')
->orWhere('meta_keywords', 'LIKE', '%' . $query . '%')
->with('categories')->orWhereHas('categories', function ($q) use ($query) {
$q->where('title', 'LIKE', '%' . $query . '%');
})
->with('documents')->orWhereHas('documents', function ($q) use ($query) {
$q->where('doc_title', 'LIKE', '%' . $query . '%');
//$q->where('doc_description', 'LIKE', '%'.$query.'%');
})
->with('tags')->orWhereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%' . $query . '%');
})
->with('images')
->orderBy('type', 'desc')
->orderBy('updated_at', 'desc')
->paginate(15);
}
在此先感谢您在此问题上的任何帮助。
【问题讨论】:
-
不幸的是,根据下面的建议,我被我所做的查询所迷惑,因为所有结果都在一组
active=1中。现在,执行一个不同的查询,其中有一篇带有active=0的文章仍然出现在结果中。总而言之,仍然存在仅获取活动文章的相同问题..
标签: laravel search eloquent relationship