Laravel: Unknown column $parameter in 'on clau

2019-02-27 09:43发布

问题:

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);
      })