[英]Laravel HasMany WhereNotIn Query
Problem: I'm trying to query a PeopleType to find all the courses where a person isn't associated.
问题:我正在尝试查询 PeopleType 以查找与某个人无关的所有课程。
I have 4 tables我有4张桌子
I have the following relationships我有以下关系
PERSON MODEL人物模型
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 MODEL 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');
}
My attempt:我的尝试:
$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();
My response:我的回复:
Integrity constraint violation: 1052 Column 'id' in IN/ALL/ANY subquery is ambiguous
违反完整性约束:IN/ALL/ANY 子查询中的 1052 列“id”不明确
People Courses Table Schematic人员课程表示意图
Schema::create('People_Courses', function (Blueprint $table) {
$table->increments('id');
$table->integer('course_id');
$table->integer('person_id');
);
PeopleType_Courses Table Schematic PeopleType_Courses 表示意图
Schema::create('PeopleType_Courses', function (Blueprint $table) {
$table->increments('id');
$table->integer('course_id');
$table->integer('people_type_id');
);
When you're working on relations that have similar column name being selected in the query you need to resolve the ambiguity by specifying the table name with the column.当您处理在查询中选择了相似列名的关系时,您需要通过指定带有列的表名来解决歧义。 eg:
例如:
$peopleType->getCourses()->whereNotIn('courses.id', function($q)... //courses.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.