Search code examples
laraveleloquentlaravel-query-builder

laravel addSelectRaw() - how to bind a variable in addSelect()?


How can I addSelectRaw() in order to bind my variables to addSelect()?

I've got this in my code:

$query->addSelect( DB::raw('MATCH(matchy.val) against ("'.addslashes($q).'") as relevance ') );

addslashes() is far less than ideal, and i should be binding to a ? instead. How can I do addSelectRaw() instead?


Solution

  • selectRaw() behaves like addSelect already - selectRaw() actually adds the columns.

    you can just do this:

    $query->selectRaw('MATCH(matchy.val) against (?) as relevance ', [$q] );
    

    (When I was searching for the answer to this question, I duckgo'ed 'laravel addselect' and couldn't find anything, so I decided to post this and answer my own question.)