問題:我正在嘗試查詢?nèi)藛T類型以查找未關(guān)聯(lián)人員的所有課程。我有 4 張桌子人人員類型課程People_CoursesPeopleType_Courses我有以下關(guān)系人物模型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();我的回答:完整性約束沖突:1052 IN/ALL/ANY 子查詢中的列“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'););
1 回答

月關(guān)寶盒
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
在處理在查詢中選擇的具有相似列名的關(guān)系時(shí),需要通過在列中指定表名來解決多義性。例如:
$peopleType->getCourses()->whereNotIn('courses.id', function($q)... //courses.id
- 1 回答
- 0 關(guān)注
- 129 瀏覽
添加回答
舉報(bào)
0/150
提交
取消