I have following 3 tables
Classrooms
id subject
RegisteredStudents
id user_id class_id
Posts
id class_id type attachment
Models
Classroom.php
public function posts()
{
return $this->hasMany(Post::class);
}
RegisteredStudents.php
public function classroom()
{
return $this->belongsTo(Classroom::class);
}
Post.php
public function classroom()
{
return $this->belongsTo(Classroom::class);
}
Now I need to get all the posts of particular class, under which a student is registered.
for example, a student is registered for class which id no is 1 in classroom table. so it will be noted on registered_students table that this particular user is registered under this particular class. Every class may have multiple posts in post table. And the user need to get all post of his class.
I'm trying something like this:
$registered_classes = RegisteredStudent::where('user_id',$user->id)
->where(function ($q) use ($class_id){
Post::where('class_id',$class_id);
})->get();
which is definitely not correct. what will be the correct query for this.