Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

How to match two columns values in relationship

I have three tables,

tests table with columns

id, testname

categories with columns

id, category_name

Questions table with columns

id, test_id, category_id,question_name, correct_option

user_tests table with columns

id,user_id,test_status,marks_scored

user_answers table with columns

id, user_test_id,user_answer

Now i am trying to display category wise analysis like total questions, correct attempts, incorrect attempts

i am getting total questions using below query,

$get_user_test_id = UserTest::where('course_id',$course_id)->where('attempt',1)->where('user_id','=',uid())->groupBy('test_id')->get(['test_id','user_test_id','test_language']);

$totaltestids = $get_user_test_id->pluck('test_id');

$testlanguages = $get_user_test_id->pluck('test_language');

$categories = 'App\QuestionCategory'::withCount(['questionslist'=> function($q) use($totaltestids,$testlanguages){
        $q->whereIn('test_id',$totaltestids)
          ->whereIn('language',$testlanguages);
      }])->orderBy('question_category_id','ASC')->get();

it is displaying in below format,

[
  {
    "question_category_id": 1,
    "question_category_name": "Ancient and Medieval Indian History",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 93
  },
  {
    "question_category_id": 2,
    "question_category_name": "Modern History and Indian National Movement",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 22
  },
  {
    "question_category_id": 3,
    "question_category_name": "Art and Culture",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 28
  },
  {
    "question_category_id": 4,
    "question_category_name": "Indian Geography - Physical, Social and Economic",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 68
  },
  {
    "question_category_id": 5,
    "question_category_name": "World Geography - Physical and Human",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 71
  },
  {
    "question_category_id": 6,
    "question_category_name": "Indian Polity and Governance",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 70
  },
  {
    "question_category_id": 7,
    "question_category_name": "All Schemes and Programmes",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 13
  },
  {
    "question_category_id": 8,
    "question_category_name": "Social Issues",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 0
  },
  {
    "question_category_id": 9,
    "question_category_name": "Basic Economics and Institutional Economy",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 3
  },
  {
    "question_category_id": 10,
    "question_category_name": "Indian Economy",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 21
  },
  {
    "question_category_id": 11,
    "question_category_name": "Ecology and Environment",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 48
  },
  {
    "question_category_id": 12,
    "question_category_name": "General Science",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 27
  },
  {
    "question_category_id": 13,
    "question_category_name": "National Current Events",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 30
  },
  {
    "question_category_id": 14,
    "question_category_name": "International Affairs",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 7
  },
  {
    "question_category_id": 15,
    "question_category_name": "Institutions - National and International",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 30
  },
  {
    "question_category_id": 16,
    "question_category_name": "Science and Technology (Current Events)",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 20
  },
  {
    "question_category_id": 17,
    "question_category_name": "Defense and Security Issues",
    "created_at": "2017-06-21 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "questionslist_count": 2
  },
 
]

Now i want to display the correct attempts the condition will be,

where(user_answers.user_answer = questions.correct_option)

How to use useranswer modal in this existing query and match different table columns in where? Please help me with this.

0 likes
5 replies
Deekshith's avatar

whereColumns is used for same table right?

MichalOravec's avatar

You can compare two columns from different tables as well.

Deekshith's avatar

Yes thank you this query is working fine now,

  $categories = 'App\QuestionCategory'::withCount(['questionslist as totalquestions'=> function($q) use($totaltestids,$testlanguages){
        $q->whereIn('test_id',$totaltestids)
          ->whereIn('language',$testlanguages);
        }])->withCount(['questionslist as csatcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
          $q->join('tests','questions.test_id','=','tests.test_id')
            ->whereIn('questions.test_id',$totaltestids)
            ->whereIn('questions.language',$testlanguages)
            ->where('tests.csat',1);
        }])->withCount(['questionslist as nocsatcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
          $q->join('tests','questions.test_id','=','tests.test_id')
            ->whereIn('questions.test_id',$totaltestids)
            ->whereIn('questions.language',$testlanguages)
            ->where('tests.csat',0);
        }])->withCount(['questionslist as csatcorrectcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('tests','questions.test_id','=','tests.test_id')
          ->join('user_answers','questions.question_id','=','user_answers.question_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages)
          ->whereIn('user_answers.user_test_id',$totalusertestids)
          ->whereColumn('user_answers.user_answer','=','questions.correct_option')
          ->where('tests.csat',1);
        }])->withCount(['questionslist as nocsatcorrectcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('tests','questions.test_id','=','tests.test_id')
          ->join('user_answers','questions.question_id','=','user_answers.question_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages)
          ->whereIn('user_answers.user_test_id',$totalusertestids)
          ->whereColumn('user_answers.user_answer','=','questions.correct_option')
          ->where('tests.csat',0);
        }])->withCount(['questionslist as csatincorrectcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('tests','questions.test_id','=','tests.test_id')
          ->join('user_answers','questions.question_id','=','user_answers.question_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages)
          ->whereIn('user_answers.user_test_id',$totalusertestids)
          ->whereColumn('user_answers.user_answer','!=','questions.correct_option')
          ->where('tests.csat',1);
        }])->withCount(['questionslist as nocsatincorrectcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('tests','questions.test_id','=','tests.test_id')
          ->join('user_answers','questions.question_id','=','user_answers.question_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages)
          ->whereIn('user_answers.user_test_id',$totalusertestids)
          ->whereColumn('user_answers.user_answer','!=','questions.correct_option')
          ->where('tests.csat',0);
        }])->orderBy('question_category_id','ASC')->get();
Deekshith's avatar

Now i got a working query like below,

$categories = 'App\QuestionCategory'::withCount(['questionslist as totalquestions'=> function($q) use($totaltestids,$testlanguages){
        $q->whereIn('test_id',$totaltestids)
          ->whereIn('language',$testlanguages);
      }])->withCount(['questionslist as correctcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('user_answers','questions.question_id','=','user_answers.question_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages)
          ->whereIn('user_answers.user_test_id',$totalusertestids)
          ->whereColumn('user_answers.user_answer','=','questions.correct_option');
      }])->withCount(['questionslist as incorrectcount'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('user_answers','questions.question_id','=','user_answers.question_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages)
          ->whereIn('user_answers.user_test_id',$totalusertestids)
          ->whereColumn('user_answers.user_answer','!=','questions.correct_option');
      }])->with(['questionslist'=> function($q) use($totaltestids,$testlanguages,$totalusertestids){
        $q->join('tests','questions.test_id','=','tests.test_id')
          ->whereIn('questions.test_id',$totaltestids)
          ->whereIn('questions.language',$testlanguages);
      }])->orderBy('question_category_id','ASC')->get()

Now i have to add total marks concept we can calculate this using below condition,

if tests.csat == 1 then total marks will be no of questions * 2.5;
if tests.csat == 0 then total marks will be no of questions * 2;

How to get total marks in above query i have joined tests table in last relationship of above query.

Please or to participate in this conversation.