IlluminateDatabaseQueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column

I have tried this code to return all students with their information in a school.

$school=School::where('admin_id',auth()->user()->id)->first();
        return $user=User::with(["studentDetails"=>function($q) use($school){
            $q->where('studentDetails.school_id','=',$school->id);
        },"subscriptionsSatus.courses"])->get();

this was my error

IlluminateDatabaseQueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'studentDetails.school_id' in 'where clause' (SQL: select * from `student_details` where `student_details`.`student_id` in (79, 81, 82, 83, 84, 85, 86, 87, 89, 90) and `studentDetails`.`school_id` = 7) in file

what I have done wrong here.. the column name is same.. as Database

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you don’t have to specify the column :

$school=School::where('admin_id',auth()->user()->id)->first();
        
return $user=User::with(["studentDetails"=>function($q) use($school){
            $q->where('school_id','=',$school->id);
        },"subscriptionsSatus.courses"])->get();

if you want to return only user having this school then :

$school=School::where('admin_id',auth()->user()->id)->first();
        
return $user=User::with(["studentDetails"=>function($q) use($school){
            $q->where('school_id','=',$school->id);
        },"subscriptionsSatus.courses"])
->whereHas('studentDetails', function($query){
 $query->where('school_id','=',$school->id);
})->get();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x