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

Laravel (5.1) query builder does not properly add bindings to a raw select statement

我正在使用 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();

这个returns没什么,但是当我直接把引号替换成

... 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']) 等的所有变体。我怎样才能使这些绑定起作用?

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

$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 添加它们,可能不会添加额外的引号。