[英]Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
[英]How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel
我在學生和部門之間有一個belongsToMany關系。 現在我想獲取部門的 subject_id 為 null 的所有學生部門。 我在下面的代碼,但它給了我以下錯誤
SQLSTATE [23000]:完整性約束違規:1052 where 子句中的列 '
id
' 不明確(SQL:從subject_id
為空且存在的departments
中選擇 *(從students
內部連接department_student
的students
中選擇 *。id =department_student
student_id
wheredepartments
)id
=department_student
department_id
和id
= 16 和students
deleted_at
為空))
$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();
我們也可以這樣使用
->whereHas('students',function($student){
$student->where('student.id', '=', Auth::id()); //Like this
})->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.