check this part in the documentation
https://laravel.com/docs/7.x/eloquent-relationships#querying-relationship-absence
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Guys iam working with a project online quiz examination.
i have registered the students in user table. it is polymorphic realted with StudentInfo Table.
i have StudenExamMain table where the exams are stored in that table with assessment id.
this is my StudentInfo table. this table is polymorphic relationship with user table. model name userprofile
id class_id section_id phno
my assessmentTable
id name timeduaration
1 A1 30mins
2 A2 40mins
StudentExamMain table
id assessment_id student_id
1 A1 1
1 A1 2
1 A1 3
what i need the out put is
to list all the students of same class, same section, and metnion he wrote the exam or not. if he wrote just written, else absent
my query
$students=User::where('user_type','student')->
WhereHas('studentuser', function ($query) use ($request) {
$query->where('assessment_id', '!=',1);
})->get();
this results empty set why?
i have 52 students in my user table. 46 students wrote the exam, only 6 students not wrote the exam. i need to list all the student, but if wrote, "written" in not write "absent" like this i planned.
i need to get this for specific exam thats why i have a where condition where('assessment_id', '!=',1)
and one more thing, i need to fetch students of specific class, specific section. so wrote like this
$students=User::where('user_type','student')->WhereHas('userprofile', function ($query) use ($request) {
$query->where([//
['class_id', $request->class_id],
['section', $request->section_id],
]);
})->
WhereHas('studentuser', function ($query) use ($request) {
$query->where('assessment_id', '!=',1);
})->get();
but for polymorphic relationship i think we cant use whereHas it shows error Please use whereHasMorph() for MorphTo relationships.
so i used
$students=User::where('user_type','student')->whereHasMorph('userprofile', function ($query) use ($request) {
$query->where([//
['class_id', $request->class_id],
['section', $request->section_id],
]);
})->
WhereHas('studentuser', function ($query) use ($request) {
$query->where('assessment_id', '!=',1);
})->get();
it shows this error Instantiation of 'Closure' is not allowed
i also tried WhereNotexists but that too shows error
$students=User::where('user_type','student')->
whereNotExists('studentuser', function ($query) use ($request) {
$query->where('assessment_id', 1);
})->get();
error
Argument 1 passed to Illuminate\Database\Query\Builder::whereNotExists() must be an instance of Closure, string given, called in
Kindly some one help please
@abdulbazith Please read this whole part of documentation, because you mix it and everytime in wrong way.
Documentation: https://laravel.com/docs/7.x/eloquent-relationships
$students = User::where('user_type','student')->whereHasMorph('userprofile', 'App\User', function ($query) use ($request) {
$query->where('class_id', $request->class_id)->where('section', $request->section_id);
})->whereDoesntHave('studentuser', function($query) use ($request) {
$query->where('assessment_id', $request->assessment_id);
})->orderBy('id', 'asc')->get();
Also your naming of relationships is very confusing.
Please or to participate in this conversation.