php - 如何解决违反完整性约束的问题 : 1052 Column 'id' in where clause is ambiguous in laravel

标签 php laravel

我有一个 属于许多 学生和部门之间的关系。 现在我想获取所有部门的subject_id为空的学生部门 .我下面的代码,但它给了我以下错误

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))


$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/

相关文章:

php - 在新标签页中打开 dompdf 从给定页面获取数据

php - 使用 get 方法自定义 URL Helper 重定向

php - 在 echo 上的 < 符号之后替换,但 var_dump 显示正确的长度

mysql - Eloquent 查询返回按二级关系排序的项目

php - Laravel GroupBy 使用数据库表列值

php - Zend ACL 是否适合所有者对内容的权限

php - 如何确定 PHP 字符串是否仅包含纬度和经度

php - Laravel Eloquent 多个表之间的关系

php - 设置下个月的付款日期(使用 PHP Carbon)

laravel - 如何连接两个laravel应用程序?