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

1

6

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?

Piggyback answered 24/4, 2020 at 9:16 Comment(0)
P
12

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.)

Piggyback answered 24/4, 2020 at 9:19 Comment(1)
@francisco - sorry for the confusion. the part in parentheses is irrelevant.Piggyback

© 2022 - 2024 — McMap. All rights reserved.