Skip to content
Advertisement

Laravel HasMany WhereNotIn Query

Problem: I’m trying to query a PeopleType to find all the courses where a person isn’t associated.

I have 4 tables

  1. People
  2. PeopleTypes
  3. Courses
  4. People_Courses
  5. PeopleType_Courses

I have the following relationships

PERSON MODEL

JavaScript

PEOPLE_TYPE MODEL

JavaScript

My attempt:

JavaScript

My response:

Integrity constraint violation: 1052 Column ‘id’ in IN/ALL/ANY subquery is ambiguous

People Courses Table Schematic

JavaScript

PeopleType_Courses Table Schematic

JavaScript

Advertisement

Answer

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. e.g:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement