1

I'm building query using Laravel query builder and I'm having an issue with MySQL functions.

I have a Mysql functions that receives the user_id and some other paramenters to calculate a user's score. This query must return only users with score above a certain value. The query is giant, so I'm pasting only the part that is causing be trouble:

$query = $query->where(DB::raw($fstr.' >= 70'));

Generates this :

AND f_user_matching_score(4, admin__user.id_user, 1, 0, 0, 0, 0, ',') >= 70 IS NULL

This:

$query = $query->where($fstr,'>=',  70));

Generates this:

AND `f_user_matching_score(4, admin__user.id_user, 1, 0, 0, 0, 0, ',')` >= 70 

Both are invalid. The first is adding this IS NULL and the second the back ticks.

How can I solve this?

2
  • 3
    Try whereRaw instead. $query->whereRaw($fstr.' >= 70'); Commented Jan 18, 2019 at 13:28
  • @aynber Perfect, it worked. Thanks. Please post an answer so I can give you the points. Commented Jan 18, 2019 at 13:31

1 Answer 1

3

You need to use whereRaw() for fully raw where queries. This way it will not expect any other variables being passed in, though you can if need be.

$query->whereRaw($fstr.' >= 70');

If you needed to pass in variables for quoting, you'd just pass them in as an array:

$query->whereRaw("f_user_matching_score(?, admin__user.id_user, ?, ?, ?, 0, 0, ?) >= 70", [4, 1, 0, 0, ',']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.