我有一个 属于许多 学生和部门之间的关系。 现在我想获取所有部门的subject_id为空的学生部门 .我下面的代码,但它给了我以下错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from
departments
wheresubject_id
is null and exists (select * fromstudents
inner joindepartment_student
onstudents
.id
=department_student
.student_id
wheredepartments
.id
=department_student
.department_id
andid
= 16 andstudents
.deleted_at
is null))
$departments=Department::where('subject_id','=',null)
->whereHas('students',function($student){
$student->where('id',Auth::id());
})
->get();
dd($departments);
任何帮助将不胜感激
最佳答案
您应该指定引用 id
的表名。以避免歧义。
$departments=Department::where('subject_id','=',null)
->whereHas('students',function($student){
$student->where('student.id',Auth::id()); //Notice the student.id instead of id
})->get();
关于php - 如何解决违反完整性约束的问题 : 1052 Column 'id' in where clause is ambiguous in laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59009871/