如何在laravel中使用多个whereHas和where

时间:2019-07-04 07:24:06

标签: laravel eloquent

我将where与多个whereHas()一起使用,但没有得到预期的结果。

这是我的代码

public function getCommunityScoreByUser($user,$category_id) {
      $question_ids = $user->answers->pluck('question_id')->unique();

      $specialities = $this->speciality
                    ->whereHas('questions', function($qry) use($question_ids){
                        $qry->whereIn('questions.id', $question_ids);
                       })
                    ->orwhereHas('caseStudies', function($qry) use($user) {
                        $qry->where('user_id', $user->id);
                       })
                    ->where('is_active', 'Y')
                    ->where('category_id',$category_id)
                    ->withCount('answers')
                    ->withCount(['answers as bestAnswerCount' => function ($query) {
                                $query->where('question_answer.is_accepted','Y');
                                }])
                    ->with(['answers' => function ($query) {
                          $query->withCount(['votes' => function ($query) {
                                $query->where('count','1');
                             }]);
                      }])
                    ->get();
      foreach ($specialities as $speciality => $key) {
        $votes_count = 0;
        foreach ($key->answers as $key1 => $value) {
          $votes_count += $value->votes_count;
        }
        $key->votesCount = $votes_count;
        $key->totalVotes = (3*$key->bestAnswerCount)+$votes_count+$key->case_study_votes_count;
      }
      return $specialities;
}  

预期结果

我想获取有疑问或案例研究的专业,并且该专业应符合条件->where('category_id',$category_id)

实际结果

$specialities表示问题或案例研究,但不检查category_id。相反,它将获取所有类别的$specialities。但是,如果我删除wherehas之一,那么它运行良好。

请帮助我找到解决方法。

2 个答案:

答案 0 :(得分:1)

您得到的结果归因于class Result extends Component { render() { return ( <div className="section-result"> <div className="container--result"> <h1 className="heading-primary--result">Results</h1> <p className="paragraph--results"> Look at the result below to see the details of the person you're searched for. </p> </div> {this.props.dataToRender} <TryAgain /> </div> ); } } 。根据您当前的说法,它将在所有有疑问的地方或有案例研究的地方(在活跃的地方等)获得所有专业。

您想要做的就是将orWhereHas限制为or语句。

为此,您可以执行以下操作:

whereHas

此操作只是将$specialities = $this->speciality ->where(function ($qry) use ($question_ids, $user) { $qry->whereHas('questions', function ($qry) use ($question_ids) { $qry->whereIn('questions.id', $question_ids); })->orwhereHas('caseStudies', function ($qry) use ($user) { $qry->where('user_id', $user->id); }); }) ->where('is_active', 'Y') ->where('category_id', $category_id) ->withCount('answers') ->withCount(['answers as bestAnswerCount' => function ($query) { $query->where('question_answer.is_accepted', 'Y'); }]) ->with(['answers' => function ($query) { $query->withCount(['votes' => function ($query) { $query->where('count', '1'); }]); }]) ->get(); 包裹在自己的位置,以便对它们进行独立处理。

答案 1 :(得分:1)

使用orWhereHas可能不符合您的期望。如果您是

->orwhereHas('caseStudies', function($qry) use($user) {
     $qry->where('user_id', $user->id);
})

为true,则将其允许。无需检查所有其他过滤器。

如果您要检查一个whereHas,则需要将它们添加到它们自己的where中,以“限制” or

->where(function($query) use ($question_ids, $user){
    $query->whereHas('questions', function($qry) use($question_ids){
        $qry->whereIn('questions.id', $question_ids);
    })
    ->orwhereHas('caseStudies', function($qry) use($user) {
        $qry->where('user_id', $user->id);
    });
})