@mstnorris There is no polymorphic relation here I suppose. Rephrase the question and provide the result you want to achieve.
Now you just have hasManyThrough for User, thus for Student and Teacher it works the same if they extend User.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I thought I was on the right track with Polymorphic relationships however I am wrong.
I would like to set up a relationship between Users, Modules and Assignments.
A teacher who is a User can set assignments, and students (also Users) have assignments through their Modules.
This is what I am thinking but
An Assignment belongs to a Module A Module hasMany Assignments
A User HasMany Modules (both Students and Teachers) A User HasManyAssignments Through Modules
But I know I am wrong. Has anyone else modelled this kind of application?
@mstnorris There is no polymorphic relation here I suppose. Rephrase the question and provide the result you want to achieve.
Now you just have hasManyThrough for User, thus for Student and Teacher it works the same if they extend User.
I haven't set up the Student and Teacher models yet, everyone is just a User for the time being.
A Teacher can add a new Assignment which belongs to them and a Module.
A Student who hasMany Modules can see the Assignments because they are studying that particular Module.
Is that clearer? My apologies, I am still working out the structure myself.
You wouldn't use a polymorphic relationship in this instance I don't think. A polymorphic would be useful for something like notes.
In an app I'm working on, notes can belong to a client, a job, a brief, an alteration. My notes table has a notable_id and notable_type field in it. When I attach a note to a client, the notable_id would be set to $client->id, and the notable_type would be set to App\Models\Client. When attaching to a job, the notable_id would be $job->id, and the notable_type would be App\Models\Job.
Essentially, any record that could belong to more than one type of parent would be a good use for a polymorphic relationship.
In your particular instance, though, a hasMany/belongsToMany relationship should be sufficient.
Teacher has many Assignment.Assignment belongs to Teacher.Assignment has many Module.Module belongs to Assignment
Teacher has many Module through Assignment
Student has many Module
Module belongs to many Student
You wouldn't even need to necessarily establish the relationship between the Student and Assignment model as you can resolve them via the Module - $student->module->assignment->name, for example.
The relationship between a user and an assignment would be different to a teacher and an assignment, only in that the student would not have permission to create assignments and modules, only to view them but you don't need to reflect that via the database modelling as such.
Hope that gets you off in the right direction, let me know if I make no sense!
@mstnorris Yeah, I get it all. It's pretty straightforward:
// User as a student
public function assignments()
{
return $this->hasManyThrough('Assignment', 'Module');
}
$student->assignments; // through the modules
// User as a teacher
public function assignmentsIssued() // not sure about this word here ;)
{
return $this->hasMany('Assignement');
}
Check the @deringer post for belongsToMany - it is probably the one you need for User-student Module relation.
At least this is how I see it. I think.
@mstnorris Your understanding of the eloquent relations is wrong. Check this out:
Model1 | Model2
hasMany or hasOne | belongsTo
belongsToMany | belongsToMany
morphMany or morphOne | morphTo
belongsToMany is many-to-many relationship with pivot table. If that's the case, then both models use this relation.
"A Teacher can teach multiple Modules" which translates to, unless I am wrong:
class Teacher extends Model {
public function modules() {
return $this->hasMany('Module');
}
...
}
The same goes for Student.
For the Module:
class Module extends Model {
public function students() {
return $this->hasMany('Student'); // or should this be belongsToMany ?
}
public function students() {
return $this->hasMany('Teacher'); // or should this be belongsToMany ?
}
public function assignments() {
return $this->hasMany('Assignment');
}
...
}
An Assignment belongs to only one Module. I think I have explained it correctly, maybe not.
You'd use hasManyThrough as the relationship between the Teacher and the Module is indirect. Your Module model would not have a teacher_id column in it, in the same way that it wouldn't have a student_id in it.
These would be two separate pivot tables or potentially a polymorphic many to many, depending on how far down the rabbit hole you're willing to venture.
@mstnorris If there can be only one teacher of a module, then yes - Teacher hasMany Module. But Student belongsToMany Module, because I suppose there are many students in a module, and a student can attends many modules. Correct?
@JarekTkaczyk that is correct apart from a Module can be taught by multiple Teachers.
@mstnorris Then definitely both are belongsToMany :)
@JarekTkaczyk and @deringer thank you for your help.
How should I go about setting up the Student and Teacher models just so I am on the right track. Right now, they don't exist. All users are just Users.
How do they relate to Assignments?
@mstnorris The way I showed above, just use one in Student model, other in Teacher model.
This is what part of my User model current looks like:
<?php namespace App;
use ...;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
public function modules()
{
return $this->belongsToMany('App\Module');
}
// I believe that this is incorrect, this should only have Assignments through the Modules that they take
public function assignments()
{
return $this->belongsToMany('App\Assignment');
}
}
My question is do the student and teacher necessarily need to be their own models? Isn't everything really just a user at the end of the day? It'll depend on your app, but is it possible just to set a type against the user of admin, teacher, or student? I'm not convinced there need to be separate models.
If the student and teacher have the ability to login to the system, it doesn't make sense to me that they would be a separate model all of their own that would essentially be a clone of the normal user model. You could establish all of the relationships in the user model anyway.
It's difficult to say though, without understanding the app's requirements. I'm not suggesting your approach is wrong, merely that based on the information I have, I don't think I approach it in that way necessarily.
@deringer I don't think they need to be either. I have started to set up roles and permissions and I think that that is the best route to go down.
The only reason why I like to think of them as separate models is that with regards to assignments, modules and all the other stuff that we're implementing, the relationships between Students and Teachers to the other models can be fairly different.
I think if you're thinking in terms of the real world that makes sense, but in terms of the application logic, maybe not so much.
Roles and permissions would be a great way to go; if you're heading where I think you are with this app, you'd need them eventually anyway.
@deringer yes that is what I think too. I have set up the roles now so that a User can be multiple things, for example they could be a Teacher and also an Admin.
I am still confused as to how to go about creating Assignments. I'm not sure if I have been clear in my explanation, or whether I am not understanding how Eloquent Relationships work but, if I may ask for your help in understanding this. Take the following for example.
A Student studies a Course at University, that Course has many Modules, and the Module in turn has many Assignments.
The Student in that case has many Assignments because they have chosen to take that Module.
How would I set up these relationships? What would my DB schema look like and the respective models?
Thank you
Remember, a student is just a user.
Table: assignment_user
id
assignment_id
user_id
Table: assignment_course
id
assignment_id
course_id
Table: course_module
id
course_id
module_id
class User extends Eloquent {
public function courses()
{
return $this->belongsToMany('App\Course');
}
}
class Course extends Eloquent {
public function users()
{
return $this->belongsToMany('App\User');
}
public function modules()
{
return $this->belongsToMany('App\Module');
}
}
class Modules extends Eloquent {
public function courses()
{
return $this->belongsToMany('App\Course');
}
public function assignments()
{
return $this->belongsToMany('App\Assignment');
}
}
class Assignment extends Eloquent {
public function modules()
{
return $this->belongsToMany('App\Modules');
}
}
Please or to participate in this conversation.