繁体   English   中英

无法在雄辩的模型中添加where子句

[英]Cannot add where clause to eloquent model

我如何将这个where clause添加到laravel eloquent model

$proxSql = "(3958 * 3.1415926 * sqrt((Latitude - '.$lat.') * (Latitude - '.$lat.') 
              + cos(Latitude / 57.29578) * cos('.$lat.' / 57.29578) * (Longitude-'.$long.') * (Longitude - '.$long.')) / 180)";
$listings->where($proxSql, '<=', 5);

返回语法错误,似乎laravel正在添加刻度。

where子句不是在此处使用的正确子句。

您将需要使用whereRaw();

https://laravel.com/docs/5.8/queries#raw-methods

$listings->whereRaw($proxSql.' <= ?', [5]);

由于您直接在查询中使用列名,因此需要使用whereRaw() 否则,它将寻找该变量$proxSql的列名。 您还应在绑定变量时对其进行绑定。

$proxSql = "(3958 * 3.1415926 * sqrt((Latitude - ?) * (Latitude - ?) 
              + cos(Latitude / 57.29578) * cos(? / 57.29578) * (Longitude - ? ) * (Longitude - ?)) / 180) <= 5";

$listings->whereRaw($proxSql, [$lat, $lat, $lat, $long, $long]);

暂无
暂无

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

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