[英]Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous
I am getting this error "Integrity constraint violation: 1052 Column 'disabled_at' in where clause is ambiguous" although in my where I added the table name to precede the column, and also in the join statement, but it still gives me the above error.我收到此错误“Integrity constraint violation: 1052 Column 'disabled_at' in where clause is ambiguous”虽然在我的位置我添加了表名在列之前,也在连接语句中,但它仍然给我上述错误. Here is that code snipet:这是代码片段:
$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 . '%');
}
Any help will be appreciated.任何帮助将不胜感激。
well, thank all you for your suggestions.嗯,谢谢大家的建议。 I figured out what the issue was by seeing the raw mysql query using your suggestions, outputting with DB::enableQueryLog()
and query->toSQL()
, the later actually is what worked for me.通过使用您的建议查看原始 mysql 查询,使用DB::enableQueryLog()
和query->toSQL()
输出,我发现了问题所在,后者实际上对我有用。 Basically the column disabled_at
works as the soft delete 'deleted_at'
, so it's either NULL or has a timestamp, therefore what was happening was that the raw query had a "...where '.disabled_at' IS NULL..." by default (unbeknown to me), and because the sections table also has a 'disabled_at' column, it did not know to which of those 2 tables disabled_at
belonged, so I had to specify the table with the following query" whereNULL('users.disabled_at')
..." and not just let the default run.基本上列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.