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

AbdulBazith's avatar

fetch records from parent with where condition in child table. problem in using "WITH" and "WHEREHAS" laravel

guys iam working with a student online examination project.

i have two tables ExamRequest and ExamCreation

staff will request for a exam to be create, and exam creation team will create an exam for that request

so here ExamRequest is parent table and ExamCreation is child table with one to one relationship

ExamRequest table with columns

id	examdate	noofques	createdstatus
1	31-10-2020	50			1
2	31-10-2020	50			1
3	31-10-2020	50			0
4	31-10-2020	50			0

in the above table total 4 request are there but only for request exam is created for another 2 request exams not created so,

my ExamCreation table with columns

id	request_id	examdate	examstatus
1	1			31-10-2020	1
2	2			31-10-2020	0

so in the above Examcreation table only two exams are created for the request and in this two exams 1 exam is completed thats why it shows examstatus and "1". and 1 exam not completed.

Let me ask my query, i need to fetch all records from ExamRequest Table with where condition as exams not created and exams created with not completed exams. so i wrote the query as

$examslist = ExamRequest::

whereHas('ExamcreatedModel', function ($query) {

    $query->where('examstatus', 0); })->get();

But the above code brings only one record because it checks for child table with examstatus 0 and brings that record only, but i need the two ExamRequests which are not created also. so iam expecting three records

two records which are not created and 2 record created but exam not completed, so i tried like this using "with"

$examslist = ExamRequest::

with(['ExamcreatedModel'=> function ($query) {

    $query->where('examstatus', 0); }])->get();

But here whats the problem is, here it gives all the records from ExamRequest Table, displays the the request for which exam completed also, that is the where condition is working only for the child table, and not for the requesttable

what i need is to fetch all records from ExamRequest Table with where condition as exams not created and exams created with not completed examsstatus.

Kindly someone suggest your idea please

0 likes
3 replies
SilenceBringer's avatar
Level 55

Hi @abdulbazith I think it should be something like

$examslist = ExamRequest::whereHas('ExamcreatedModel', function ($query) {
    $query->where('examstatus', 0);
})
    ->orWhereDoesntHave('ExamcreatedModel')
    ->get();
1 like
AbdulBazith's avatar

@silencebringer thank you for your response.

i tried the way u gave the code it fetched empty results

but i shifted it then it worked correctly may i know what may be the problem


$examslist = ExamRequest:: DoesntHave('ExamcreatedModel')->

orwhereHas('ExamcreatedModel', function ($query) {

    $query->where('examstatus', 0);

})
   
    ->get();

this gave the correct output . may i know the difference between "wheredoesnthave" and "doesnthave" ??

Please or to participate in this conversation.