I Have three tables,
user_tests => user_test_id, user_id, marks_scored,time_taken, language
user_answers => user_answer_id, user_test_id, user_answer, time_taken, question_id,
question_table => question_id, question_title,description,option_a, option_b,option_c, option_d,correct_option, language
I have relationsip like below,
UserTest.php
public function fetchanswerslist()
{
return $this->hasMany('App\UserAnswer','user_test_id','user_test_id');
}
User Answer.php
public function fetchquestion()
{
return $this->belongsTo('App\Question','question_id','question_id');
}
So in controller i used below code,
$usertests = UserTest::with(['fetchanswerslist.fetchquestion'])->where('course_id',$course_id)->where('user_id','=',uid())->get();
Now i want to write the condition like user_tests.language = questions.language
i have writeen a query like below but i want to rewrite using relationsip,
$usertests = UserTest::where('course_id',$course_id)->where('user_id','=',uid())->get();
$test_array =array();
$language_array =array();
foreach($usertests as $get_user) {
array_push($arr_ids,$get_user->user_test_id);
array_push($test_array,$get_user->test_id);
array_push($language_array,$get_user->test_language);
}
$ids = join("','",$arr_ids);
$languageids = join("','",$language_array);
//easy questions
$cat['easy_correct_attempt'] = DB::table('user_answers')
->leftjoin('questions','user_answers.question_id','=','questions.question_id')->leftjoin('question_categories','questions.question_category','=','question_categories.question_category_id')->leftjoin('user_tests','user_answers.user_test_id','=','user_tests.user_test_id')
->whereRaw(" user_answers.user_answer = questions.correct_option"
. " and questions.question_level = 'Easy' and user_tests.user_id=$ckeck_user_id and user_tests.user_test_id in ('$ids') and questions.language in ('$languageids')")
->count();
How to write above query in relationship? as i want match the column value of base table with join/relationship table