hasManyThrough关系中未发现问题

时间:2019-10-25 08:47:38

标签: laravel

在我的SpecialityMaster模型中,我有这些关系

 public function questions()
 {
    return $this->belongsToMany('App\Models\Question','question_speciality','speciality_id','question_id');
  }

  public function answers()
  {
      return $this->hasManyThrough('App\Models\QuestionAnswer', 'App\Models\QuestionSpeciality','speciality_id','question_id','id','question_id');
  }    

我有此代码 SpecialityMasterRepository

    $specialities = $this->speciality
                                ->withCount(['answers'=> function ($query) use($user,$question_ids){
                                    $query->where('question_answer.is_active','Y')
                                          ->where('question_answer.user_id',$user->id)
                                          ->where(function($query) use ($question_ids, $user){
                                              $query->whereHas('question', function($qry) use($question_ids){
                                                  $qry->whereIn('questions.id', $question_ids)
                                                      ->whereColumn('specialities_master.id','questions.primary_speciality');
                                              });
                                          });
                                }])

                ->with(['answers' => function ($query) use($user,$question_ids,$answer_ids){
                            //$query->whereColumn('specialities_master.id','question_answer.question_id')
                                         ->where(function($query) use ($question_ids, $user){
                                                   $query->whereHas('question', function($qry) use($question_ids){
                                                       $qry->whereIn('questions.id', $question_ids)
                                                           ->whereColumn('specialities_master.id','questions.primary_speciality');
                                                   });
                                               })
                ->withCount(['votes' => function ($query1) use ($answer_ids) {
                              $query1->where('count','1')
                              ->whereIn('question_answer_votes.votable_id', $answer_ids)
                              ->where('question_answer_votes.votable_type','App\Models\QuestionAnswer');
                          }])
                      ->whereIn('question_answer.question_id', $question_ids)
                      ->where('question_answer.is_active','Y')
                      ->where('question_answer.user_id',$user->id);

            }])   

我能够在 withCount('answers')中进行-> whereColumn('specialities_master.id','questions.primary_speciality')的操作,但是在 with('answers')中却无法进行同样的操作。

它显示此错误:-

  

找不到列:1054“ where子句”中的未知列“ specialities_master.id”

任何解决此错误的想法对我都会有很大帮助。

注释
实际上,我想要的是仅提取与 questions 表的 primary_speciality 字段与 specialities_master 表id匹配的答案(/答案票)

questions table           :- id, user_id, title, primary_speciality    
question_answer table     :- id, user_id, question_id, answer    
question_speciality table :- id, question_id, speciality_id    
specialities_master table :- id, title

谢谢。

0 个答案:

没有答案