Laravel: Unknown column $parameter in 'on clause'

倖福魔咒の 提交于 2019-12-04 04:52:19

问题


I have troubles with using a parameter in a DB query and parameter binding in Laravel.

I get this error:

Error: "Column not found: 1054 Unknown column '3' in 'on clause'"

This is the part of a query:

->join('foo AS f1', function($join) use ($bar)
      {
           $join->on('f1.foo', '=', 'f2.foo')
                ->on('f1.bar', '=', $bar);
      })

If I do this instead, it works:

->on('f1.bar', '=', DB::raw($bar));

What's the solution to this? I would like to use parameter binding for this as well of course. However, when I do:

->on('f1.bar', '=', ':bar', ['bar' => $bar]);

I get this:

ErrorException in Grammar.php line 196:
Array to string conversion

回答1:


when you connecting two tables with join you must specify the column names ($bar must be equal to column name string). So if you want to send some parameters data you must use where instead on.

->join('foo AS f1', function($join) use ($bar)
      {
           $join->on('f1.foo', '=', 'f2.foo')
                ->where('f1.bar', '=', $bar);
      })


来源:https://stackoverflow.com/questions/44952290/laravel-unknown-column-parameter-in-on-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!