简体   繁体   English

Laravel 5.4查询构建器和MySQL函数

[英]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. 我正在使用Laravel查询构建器构建查询,我遇到了MySQL函数的问题。

I have a Mysql functions that receives the user_id and some other paramenters to calculate a user's score. 我有一个Mysql函数接收user_id和一些其他参数来计算用户的分数。 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. 第一个是添加这个IS NULL ,第二个是后面的滴答。

How can I solve this? 我怎么解决这个问题?

You need to use whereRaw() for fully raw where queries. 您需要使用whereRaw()完全原始查询。 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, ',']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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