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

Deekshith's avatar

Filter questions count based on language in relationship

I have tables like below,

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, language

user_tests table with columns

id,user_id,test_status,marks_scored,test_language

user_answers table with columns

id, user_test_id,user_answer

Now i am trying to display category wise analysis like total questions

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
  },
 
]

And here questions table consists both english and hindi (another local language) for same test so if user takes one test in hindi and another in english then the above query displaying the question count *2 becuase pluck of language displays both language.

Now i want to get questions of user_test.test_language only. i tried using join like below,

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

But no use it shows wrong data please check soon.

0 likes
1 reply

Please or to participate in this conversation.