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

Ligonsker's avatar

Ambiguous column name when using selectRaw, coalesce and join

I have the following join query:

$data = Table1::selectRaw("COALESCE(table1.name, 'empty') AS name")
->leftJoin("table2", "table1.id", "=", "table2.id")
->get()

I specifically chose the name from table 1 yet I still get ambiguous column error

Why?

0 likes
5 replies
Sinnbeck's avatar

Show the output of

$data = Table1::selectRaw("COALESCE(table1.name, 'empty') AS name")
->leftJoin("table2", "table1.id", "=", "table2.id")
->dd()
Ligonsker's avatar

@Sinnbeck I'm sorry, I forgot that I had a groupBy, where I also needed to add it: groupBy('table1.name', ..)

Tray2's avatar

This means that you have two columns with the same name

Ambiguous column name

you need to specify which table it comes from.

Please or to participate in this conversation.