Hello, i want to do something like that:
(Teachers and Students are in Users table)
Many Students can have many Classes depending on the year
| User | Class | Year |
| ------- | ----- | ------- |
| John | 1 | 2016 |
| John | 2 | 2017 |
| John | 1 | 2018 |
| Doe | 1 | 2016 |
| Doe | 1 | 2017 |
| ... | ... | ... |
Many Teachers can have many Classes depending on the year
| User | Class | Year |
| -------- | ----- | ------- |
| Mr Smith | 1 | 2016 |
| Mr Smith | 2 | 2016 |
| Mr Smith | 3 | 2016 |
| Mr Smith | 1 | 2017 |
| ... | ... | ... |
So, many years have many students and classes
I want to be able to get for example:
- a list of students of Mr Smith for year 2016
- a list of students of class #4 for year 2016
- ...
Should I make pivot table with 3 tables like : class_user_year ?
Or i can make that with manytomany relations, with 2 pivot tables like :
User model
public function classes(){
return $this->hasMany('App\Classe', 'class_user', 'user_id', 'class_id');
}
public function years(){
return $this->hasMany('App\Year', 'year_user', 'user_id', 'year_id');
}
Class model
public function users(){
return $this->hasMany('App\User', 'class_user', 'class_id', 'user_id');
}
public function years(){
return $this->hasMany('App\Year', 'class_year', 'class_id', 'year_id');
}
Year model
public function users(){
return $this->hasMany('App\User', 'year_user', 'year_id', 'user_id');
}
public function classes(){
return $this->hasMany('App\Classe', 'class_year', 'year_id', 'class_id');
}
Thanks for your time.
Som