столбец не найден Проблема в отношении hasManyThrough - PullRequest
0 голосов
/ 25 октября 2019

В моей 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);

            }])   

Я смог сделать -> whereColumn('specialities_master.id','questions.primary_speciality') в withCount ('answers') , но не может сделать то же самое в with ('answers') .

Показывает эту ошибку: -

Столбец не найден: 1054 Неизвестный столбец 'specialities_master.id' в 'where clause'

Любые идеи по устранению этой ошибки будут мне очень полезны.

Примечания
На самом деле я хотел получить только те ответы (/ ответы), которые соответствуют primary_speciality поле вопросов таблица кid specialities_master table

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

Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...