繁体   English   中英

Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确

[英]Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous

我收到此错误“Integrity constraint violation: 1052 Column 'disabled_at' in where clause is ambiguous”虽然在我的位置我添加了表名在列之前,也在连接语句中,但它仍然给我上述错误. 这是代码片段:

        $name = ($params['query']) ? $params['query'] : '';
        $perPage = (isset($params['perpage']) && $params['perpage'] !== null) ? 
            intval($params['perpage']) : 50;

        $query = $this->with('sections');
              
        // search disabled users
        if ($disabled) {
            $query->withTrashed();
            // if not all, limit to disabled only
            if (!$allUsers) {
                $query->whereNotNull('users.disabled_at');
            }
        }

        // exclude name search when blank to increase query speed
        if ($name !== '') { 
            $query->join('sections', 'users.section_id', '=', 'sections.id');
            $query->where(DB::raw("CONCAT(users.title,' ',users.first_name,' ',users.last_name,' ',users.email,' ',sections.name)"), 'like', '%' . $name . '%');
        }

任何帮助将不胜感激。

嗯,谢谢大家的建议。 通过使用您的建议查看原始 mysql 查询,使用DB::enableQueryLog()query->toSQL()输出,我发现了问题所在,后者实际上对我有用。 基本上列disabled_at用作软删除'deleted_at' ,所以它要么是 NULL 要么有时间戳,因此发生的事情是原始查询默认有一个“... where '.disabled_at' IS NULL ...” (我不知道),并且因为 sections 表也有一个 'disabled_at' 列,它不知道disabled_at属于这 2 个表中的哪一个,所以我不得不使用以下查询指定表" whereNULL('users.disabled_at') ..." 而不仅仅是让默认运行。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM