[英]Laravel HasMany WhereNotIn Query
问题:我正在尝试查询 PeopleType 以查找与某个人无关的所有课程。
我有4张桌子
我有以下关系
人物模型
public function getPeopleType() {
return $this->belongsTo('App\PeopleType','type_id');
}
public function getCourses() {
return $this->belpngsToMany('App\Course','People_Courses','person_id','course_id');
}
PEOPLE_TYPE 模型
public function getPeople() {
return $this->hasMany('App\Person','type_id');
}
public function getCourses() {
return $this->belpngsToMany('App\Course','PeopleType_Courses','people_type_id','course_id');
}
我的尝试:
$peopleType = \App\PeopleType::FindOrFail(1);
$courses = $peopleType->getCourses()->whereNotIn('id', function($q) use($person) {
$q->select('course_id')
->from('People_Courses')
->where('person_id', $person->id);
})->get();
我的回复:
违反完整性约束:IN/ALL/ANY 子查询中的 1052 列“id”不明确
人员课程表示意图
Schema::create('People_Courses', function (Blueprint $table) {
$table->increments('id');
$table->integer('course_id');
$table->integer('person_id');
);
PeopleType_Courses 表示意图
Schema::create('PeopleType_Courses', function (Blueprint $table) {
$table->increments('id');
$table->integer('course_id');
$table->integer('people_type_id');
);
当您处理在查询中选择了相似列名的关系时,您需要通过指定带有列的表名来解决歧义。 例如:
$peopleType->getCourses()->whereNotIn('courses.id', function($q)... //courses.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.