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

AbdulBazith's avatar

fetch students from user table who not wrote specific exam(where comdtion) in exam table to get the absent list

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

0 likes
6 replies
MichalOravec's avatar

Look here, you can see that whereNotExists work with subquery.

Next thing whereHaswork with relationship as you can see here.

So does your User model relationship studentuser?

AbdulBazith's avatar

@tray2 @michaloravec thank you so much guys for your response.

i did like this and i got the solution.. whether this is right??

   $absentees = User::where('user_type','student')->doesntHave('studentuser', 'and', function($query) use ($request){
            $query->where('assessment_id', $request->assessment_id);
        })->orderBy('id','asc')->get();

fetch records from user table where user_type= student and check those students have records in 'studentuser' model for the assessment_id=$request->assessment_id

i have my relation like this User Model

  public function userprofile()
    {
        return $this->morphTo();
    }

    public function studentuser()
    {
        return $this->hasMany('App\StudentExamMain', 'student_id');
    }

My studentExamMain model

 public function user()
    {
        return $this->belongsTo('App\User', 'student_id');
    }

  public function assessmentmaindetails()
    {
        return $this->belongsTo('App\AssessmentCreationMain','assessment_id');
    }

But this query gives correct result. and my doubt is, i need to write a query for polymorphic relation.


$students=User::where('user_type','student')->WhereHas('userprofile', function ($query) use ($request) {
                $query->where([//      
         ['class_id',  $request->class_id],
         ['section',   $request->section_id],
     ]);
        })->doesntHave('studentuser', 'and', function($query) use ($request){
            $query->where('assessment_id', $request->assessment_id);
        })->orderBy('id','asc')->get();


for polymorphic i used like this and it showed error

Please use whereHasMorph() for MorphTo relationships.

so i changed WhereHas to WhereHasMorph but still showed error

Instantiation of 'Closure' is not allowed

what my expectation is to fetch records from user table where user_type='student' and check those student belongs to this class, this section in userprofile model and then check in studentuser model those students records are there or not for specific assessment_id

My user model

public function userprofile()
    {
        return $this->morphTo();
    }

    public function studentuser()
    {
        return $this->hasMany('App\StudentExamMain', 'student_id');
    }

My StudentRegistration model

 public function user()
    {
        return $this->morphOne('App\User', 'userprofile');
    }


any help please???

MichalOravec's avatar
Level 75

@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.

AbdulBazith's avatar

@michaloravec thank you soo much. and sorry for delay. the given code worked perfectly..

thank you. if possible kindly answer my other threads please..

Please or to participate in this conversation.