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

AbdulBazith's avatar

Laravel eloquent for fetch records for each category within given limit

Hello guys iam working with a online exam project i have a question_table which has topic_id as foreign key with questions in it and i have a assessment table. once a student creates assessment for a given topic with question count like 20 means it will be created and saved in this assessment table

whats my doubt is

i have a form that allows students to choose N number of topics like 2 or 3 or so on.. and question count also he will choose that we have option 10,20,30..

Now what i expect is from the choosen topic assessment should be created, say for example if student choose 4 topics and 30 question count means it should be equally take questions from each topic from question_table like 7 questions from each topic (4 tpoics ) and 2 question can be taken randomly so it will be 30 count. and also it need to check in the assessment table that those questions should not be there.. that means if the student wrote exam in the same topic yesterday means those questions will be saved in assessment table, those questions should be neglected and new questions should be fetched from those 4 topics equally each more or less..

Kindly provide a solutions totally 3 conditions

  1. question count and topic count is fixed by student
  2. based on the total question count questions must be fetched equally with the selected topics
  3. those questions must not be there in assessment table for that auth->user

How i can achieve this.

and i wrote a long code for this and achived. but the code is too long , so when student try to write exam its loading loading loading taking too long time to generate the question mean while student pressing the submit button multiple times, so lots of request issued to the server..

Kindly provide a query and also for this multiple pressing of student

0 likes
3 replies
Snapey's avatar

Is it a consideration that one topic has more questions available than another?

I had this problem in the past where topic 1 had 20 questions and topic 2 had 5 questions. Student wants 20 questions from 4 topics (5 from each). This means that topic 2 will be represented by the same 5 questions every time

Is this a concern?

AbdulBazith's avatar

@snapey ha yes yes. the same issue. sometimes there may be very less question in each topic, not exactly question need to be spliited. as you said if topic 2 has only 5 question then once the 5 question is picked it should leave that topic..

what i have wrote in my logic is

  $qqid=array();    // this i intialized a array to put the selected questions becuase  to use in wherenotin it should not check the same question again

for loop $request->topic_id
{
here i used take(1) from each topic ->and i used wherenotin(question_id, $qqid);
another foreach
{
if
count($eachques->assmnttable->where('login_user_id',auth()->id()))==0  -> here iam checking whether that question is already wrote by the student
that queston can be taken and saved in DB assessment table and it should be putin  $qqid
else
that question is not taken and it should be put in  $qqid
}

}

this is my login i used 3 foreach and i didnt mention the top foreach.. there i will check the total question count how many question to be choosen by student 20 or 30..

But this takes toooooo long time to execute..

can you suggest any idea please

Please or to participate in this conversation.