我有一个用户模型,列有id、name、email和
列id、名称、代码和
一个枢轴表course_user。
课程与使用者有多对多的关系。
course_user表包含列course_id、user_id、节
在选修课程时,部分用户将被列为枢轴栏目中的“待定”,而一些用户将被列为“A”、“B”等。
问题:我想得到至少有一个用户参加的课程,到目前为止我已经尝试了以下几种方法:在课程模型中:
public function users(){
return $this->belongsToMany(User::class)
->withPivot('section');
->wherePivot('section', 'Pending');
}我在课程模型中尝试过的另一种方法是:
public function pendingUsers(){
return $this->belongsToMany(User::class)
->withPivot('section')
->wherePivot('section', 'like', 'Pending');
}但所有这些都给了我所有的课程。现在,如何才能完成这项工作呢?
发布于 2018-01-23 09:27:11
定义关系:
public function users()
{
return $this->belongsToMany(User::class)->withPivot('section');
}然后:
Course::whereHas('users', function($q) {
$q->where('course_user.section', 'Pending');
})
->get();https://stackoverflow.com/questions/48398139
复制相似问题