【发布时间】:2020-07-03 03:13:19
【问题描述】:
我在用户模型和课堂模型之间存在多对多关系
这是结构:
- users
- id
- name
- classroom_user
- classroom_id
- user_id
- is_teacher
- classrooms
- id
- name
这是关系:
用户模型:
public function classrooms() {
return $this->belongsToMany(Classroom::class)->withPivot('is_teacher');
}
课堂模式:
public function users() {
return $this->belongsToMany(User::class)->withPivot('is_teacher');
}
public function teachers() {
return $this->belongsToMany(User::class)->where('is_teacher','=',1);
}
public function students() {
return $this->belongsToMany(User::class)->where('is_teacher','=',0);
}
我想通过获取 is_teacher 列的值来检查当前登录的用户是否是当前教室的老师。
目前正在这样做:
auth::user()->classrooms->find($classroom->id)->pivot->is_teacher
有没有更好的方法来做到这一点?
【问题讨论】: