Eloquent\'s where()
seems not working when comparing two column values. How to fix it?
Sample code:
->where(\'table_1.name\', \'=\',
Escaping is unnecessary in this case, you can use whereRaw()
:
->whereRaw('table_1.name = table_2.name')
I figured it out. 'table_2.name'
is interpreted as plain string and not a mysql table column.
Possible solutions:
Wrap 'table_2.name'
with \DB::raw()
->where('table_1.name', '=', \DB::raw('table_2.name'))
Wrap the entire expression with whereRaw()
(based on @limonte's answer)
->whereRaw('table_1.name = table_2.name')
You can use where column:
->whereColumn('table_1.name', 'table_2.name')