Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dillscher's avatar

How to prevent ambigous on Eloquent based query?

Hi,

I'm trying to figure out how to extend a builder query to join with another table and by this other table. Unfortunately both tables "articles" and "users" have a column named "lang", which results in an ambigous error.

        $articles = Article::published()
                    ->lang()
                    ->with('genre')
                    ->filter($request)
                    ->get();

and here is the filter($request) part to extend it

            return $builder->with('creator')
                ->join('users', 'users.id', '=', 'articles.user_id')
                ->orderBy('users.last_name','asc')
                ->orderBy('users.first_name','asc')
                ->select('articles.*')
                ;

How to fix the resulting error?

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'lang' in where clause is ambiguous (SQL: select `articles`.* from `articles` inner join `users` on `users`.`id` = `articles`.`user_id` where `is_draft` = 0 and `lang` = en order by `users`.`last_name` asc, `users`.`first_name` asc)

I could not find any way but disabling the ->lang() attribute which I do need for other queries, too.

Maybe somebody could give me a little tip?

Cheers

Chris

0 likes
2 replies
Talinon's avatar
Talinon
Best Answer
Level 51

On your Article model, update the lang() query scope to be explicit.

public funtion scopeLang($query)
{

    return $query->where($this->getTable() . '.lang', 'value');

}
dillscher's avatar

Wow, that was fast and unexpectedly simple ;-).

Thank you very much. It solved my issue and I can move on.

Please or to participate in this conversation.