'id' column with join
I'm having a Column 'id' in where clause is ambiguous when i use join to filter the query:
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->join('enrollments', 'users.id', '=', 'enrollments.user_id')
->leftJoin('applications', 'users.id', '=', 'applications.user_id')
->select(
'users.*',
'enrollments.section_id',
'enrollments.school_class_id',
'enrollments.status',
'applications.application_id',
'applications.class_of_entry_id',
'applications.screening_mark',
'applications.screening_status'
)
->where('users.role_id', Role::STUDENT);
}
The database doesn't know which column to choose the 'id' from. This only gives error in edit and view $operation. Any help to fix this??4 Replies
Can you be more specific? There’s 7 different ids in the code you shared and most likely there’s a relationship that is off.
The resource is 'users'. But the database doesn't know which id to use in edit $operation for the user resource since it's joined to two other models. Hope it's clear now...
?
What's the actual query? Where does the
id
come from. I think we usually use the fully qualified key for db queriesFor the getEloquentQuery(): above i get SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous error when i want to edit a row.
the SQL is
AND
id = 892
but i think it's expecting users.id due to join tables