lundi 25 juillet 2016

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

I'm using Laravel 5.1 and have the following simple query:

$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();

This returns nothing, but when I directly replace the quotation mark with

... AGAINST('$searchTerm*' IN BOOLEAN MODE) ...

then the results are correct. However, if I do

DB::getQueryLog();

I get

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

so it's as if the bindings should be added, but they're not. I have tried all variations of select, selectRaw, ->setBindings, ->addBinding($searchTerm, ['select']) etc. that have been suggested elsewhere. How can I make these bindings work?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire