Laravel(5.1)查询构建器未正确地将绑定添加到原始select语句

时间:2016-07-25 14:15:09

标签: php mysql laravel laravel-5.1

我正在使用Laravel 5.1并具有以下简单查询:

$searchTerm = 'random word';

$subQuery = DB::table('userprofile')->selectRaw("
    user_id,
    MATCH(first_name, last_name) AGAINST('?*' IN BOOLEAN MODE) AS search_score
")
->addBinding($searchTerm)
->get();

这会返回 nothing ,但是当我用

直接替换引号时
... AGAINST('$searchTerm*' IN BOOLEAN MODE) ...

然后结果是正确的。但是,如果我这样做

DB::getQueryLog();

我得到了

"query" => "select `user_id`, MATCH(first_name, last_name) AGAINST('?*' IN BOOLEAN MODE) AS search_score from `userprofile`"
"bindings" => array:1 [
  0 => "random word"
]

所以就好像应该添加绑定,但它们不是。我已尝试过select,selectRaw, - > setBindings, - > addBinding($ searchTerm,['select'])等所有其他方面的建议。如何使这些绑定工作?

1 个答案:

答案 0 :(得分:1)

你试过像这样替换整个正则表达式吗?

$searchTerm = 'random word*';

$subQuery = DB::table('userprofile')->selectRaw("
    user_id,
    MATCH(first_name, last_name) AGAINST(? IN BOOLEAN MODE) AS search_score
")
->addBinding($searchTerm)
->get();

这样,如果laravel添加它们,可能不会添加额外的引号。