简体   繁体   中英

Laravel 5.4 query builder and MySQL functions

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?

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, ',']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM