簡體   English   中英

如何解決完整性約束違規:1052 列 'id' in where 子句在 laravel 中不明確

[英]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_studentstudents中選擇 *。id = department_student student_id where departmentsid = department_student department_idid = 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM