gpt4 book ai didi

laravel - 将变量绑定(bind)到原始查询

转载 作者:行者123 更新时间:2023-12-02 13:46:40 24 4
gpt4 key购买 nike

我正在尝试将变量绑定(bind)到我的原始查询:

$data = $myModel->select( DB::raw('(3959 * acos(cos(radians(:lat)) * cos(radians(lat)) * cos(radians(lng) - radians(:lng)) + sin(radians(:lat)) * sin(radians(lat)))) as distance'), array(
'lat' => $lat, 'lng' => $lng,
))->orderBy('distance', 'ASC')->having('distance', '<', $radius)
->get();

我检查了导致这篇博文的各种 SO 帖子:

http://fideloper.com/laravel-raw-queries

我仍然收到错误:

strtolower() expects parameter 1 to be string, array given

我哪里出错了?

最佳答案

您必须将变量绑定(bind)到raw方法而不是select:

$select = "(3959 * acos(cos(radians(:lat)) * cos(radians(lat)) * cos(radians(lng) - radians(:lng)) + sin(radians(:lat)) * sin(radians(lat)))) as distance";
$data = $myModel->select( DB::raw($select,array(
'lat' => $lat, 'lng' => $lng,
)))->orderBy('distance', 'ASC')->having('distance', '<', $radius)
->get();

或者直接使用selectRaw方法:

$data = $myModel->selectRaw($select,array(
'lat' => $lat, 'lng' => $lng,
))->orderBy('distance', 'ASC')->having('distance', '<', $radius)
->get();

关于laravel - 将变量绑定(bind)到原始查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com