简体   繁体   English

如何解决完整性约束违规:1052 列 'id' in where 子句在 laravel 中不明确

[英]How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel

I have a belongsToMany relationship between the students and departments.我在学生和部门之间有一个belongsToMany关系。 Now i want to fetch all the departments of students where subject_id of departments is null .现在我想获取部门的 subject_id 为 null 的所有学生部门 I below code but it gives me the following error我在下面的代码,但它给了我以下错误

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from departments where subject_id is null and exists (select * from students inner join department_student on students . id = department_student . student_id where departments . id = department_student . department_id and id = 16 and students . deleted_at is 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);

Any help will be appreciated任何帮助将不胜感激

You should specify the table name where you are referencing the id to avoid the ambiguity.您应该指定引用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();

We also can use like this我们也可以这样使用

->whereHas('students',function($student){ 
      $student->where('student.id', '=', Auth::id()); //Like this
  })->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 完整性约束违规:1052 where子句中的列'id'不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 违反完整性约束:1052 where子句不明确的列'prof_id'Laravel - Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous 完整性约束违规:1052 列 'group_id' 在 where 子句不明确 - Integrity constraint violation: 1052 Column 'group_id' in where clause is ambiguous 违反完整性约束:1052 order子句中的“位置”列不明确 - Integrity constraint violation: 1052 Column 'position' in order clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM