![](/img/trans.png)
[英]Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous
[英]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.