简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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