【问题标题】:Laravel Eloquent query ignoring column valueLaravel Eloquent 查询忽略列值
【发布时间】: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


【解决方案1】:

结果是因为您使用了 orWhere 条件。为了修复它使用这个

$this->where('active', 1)
     ->where('start_publishing', '<=', Carbon::now())
     ->where(function($q) use ($query) {
         $q->where('external_reference', 'LIKE', '%' . $query . '%') // the $q here is required
         ->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);

【讨论】:

  • 嗨,是的,现在它甚至不起作用。它抛出异常"Class App\Models\Article does not exist"
  • -&gt;orWhere('meta_keywords', 'LIKE', '%' . $query . '%');中修复这个;
  • 我已经对您的答案进行了编辑,因为它有一个错误。现在,使用您的建议和修复的错误,它可以按预期工作。谢谢@davit!
【解决方案2】:

好的,在 Davit 的建议下,一开始似乎可行,但正是我所做的查询“伪造”了结果,我终于设法让它发挥作用。

采用 David 建议的代码,我通过查询的构造逻辑进行了一些更改,并最终得到了一个最终工作方法,该方法将仅返回与请求中的“查询”条件匹配的活动文章。最终方法代码下方:

// Method in the Article Model class
public function getSearchResults($query)
{
    return $this->where('active', 1)
            ->where('start_publishing', '<=', Carbon::now())
            ->where(function ($q) use ($query) {
                $q->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 . '%');
                $q->with('categories')->orWhereHas('categories', function ($q) use ($query) {
                    $q->where('title', 'LIKE', '%' . $query . '%');
                });
                $q->with('documents')->orWhereHas('documents', function ($q) use ($query) {
                    $q->where('doc_title', 'LIKE', '%' . $query . '%');
                    //$q->where('doc_description', 'LIKE', '%'.$query.'%');
                });
                $q->with('tags')->orWhereHas('tags', function ($q) use ($query) {
                    $q->where('name', 'LIKE', '%' . $query . '%');
                });
            })
            ->with('categories')
            ->with('tags')
            ->with('images')
            ->distinct()
            ->orderBy('type', 'desc')
            ->orderBy('updated_at', 'desc')
            ->paginate(15);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2014-12-10
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    相关资源
    最近更新 更多