【问题标题】:passing parameters to a method in a query using whereRaw使用 whereRaw 将参数传递给查询中的方法
【发布时间】:2016-08-18 22:23:46
【问题描述】:

如何使用 whereRaw 将参数传递给查询中的方法?

尝试添加列名“纬度”和“经度”时出现“语法错误,意外的“。”:

public function show($lon, $lat, $radio) {
  $results = Viaje::where($otherStuff)
                  ->whereRaw( 'radio + ' . $radio . ' <= ' . $this->getDistanceFromLatLonInKm($lat, $lon, . 'latitude, longitude)');
  return response()->json($results);
}

如果我删除那个点,我最终会将 2 个浮点数 + 2 个字符串传递给我的方法,而不是需要 4 个浮点数,所以我得到错误 500。

public function show($lon, $lat, $radio) 
{   
    $results = Viaje::where($otherStuff)->whereRaw( 'radio + ' . $radio . ' <= ' . $this->getDistanceFromLatLonInKm($lat, $lon, 'latitude', 'longitude'));   
    return response()->json($results); 
}

编辑:

“radio”、“longitude”和“latitude”是包含浮点值的列名。

public function getDistanceFromLatLonInKm($lat1,$lon1,$lat2,$lon2) {
  $R = 6371; // Radius of the earth in km
  $dLat = deg2rad($lat2-$lat1);
  $dLon = deg2rad($lon2-$lon1);
  $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2))
       + sin($dLon/2) * sin($dLon/2);
  $c = 2 * atan2(sqrt($a), sqrt(1-$a));
  $d = R * c; // Distance in km
  return $d;
}

【问题讨论】:

  • getDistanceFromLatLonInKm($lat1, $lon1, $lat2, $lon2)
  • $results = Viaje::where($otherStuff)->whereRaw( 'radio + ' . $radio . ' getDistanceFromLatLonInKm($lat, $lon, '纬度', '经度'));
  • 你只传递浮点数和字符串.....$lat, $lon浮点数和`'纬度','经度'`字符串?
  • 我需要通过 4 个浮点数。 $lat1 和 $lon1 很简单,因为它们是我的 Show 方法接收的变量,但不确定如何传递 $lat2 和 $lon2 因为它们都是我的“纬度”和“经度”列中的值。
  • 可以发一下表结构吗?

标签: php laravel-5 where


【解决方案1】:

试试这个:

public function show($lon, $lat, $radio) 
{   
    $results = Viaje::where($otherStuff)->selectRaw('*,(
                              6371 * acos (
                              cos ( radians($lat) )
                              * cos( radians( latitude ) )
                              * cos( radians( longitude ) - radians($lon) )
                              + sin ( radians($lat) )
                              * sin( radians( latitude ) )
                            )       
                    )AS distance')
                    ->whereRaw(radio + ' . $radio . ' <= distance')
                    ->get();
    return response()->json($results); 
}

【讨论】:

    【解决方案2】:

    我假设 radio 是这个纬度和经度列所在的表名,如果不是这样,则将 radio 替换为表名代替 radio.longitude 和 radio.latitude 到 tablename.latitude 和 tablename.longitude

    public function show($lon, $lat, $radio) {
                $results = Viaje::where($otherStuff)
                ->whereRaw( 'radio + ' . $radio . ' <= ' .$this->getDistanceFromLatLonInKm($lat, $lon,radio.latitude, radio.longitude));
    return response()->json($results);
     }
    

    【讨论】:

    • 我刚刚编辑了我的帖子,说“无线电”、“经度”和“纬度”是列名。表名是 Viaje。
    • 和 Viaje.latitude 不起作用,给我错误 500。
    • 什么是无线电以及您在 $radio 中传递的值是假设无线电是您正在应用的列名称您可以在哪里添加相关的表模式,以便我可以写下查询它
    • 收音机也是一个浮点数。对于每个寄存器,我都有一个无线电、经度和纬度的值。 $lon、$lat 和 $radio 是用户本身的变量,并且希望将这些值与整个表进行比较并检索完成该句子的那些。
    • 您能否添加两到三行的示例数据并传递值并告诉我您想要的实际输出?我的意思是一个例子,因为我无法理解你为什么在 where 子句左侧参数中执行 'radio +'.$radio 始终是一列,但在你的情况下,双方都在传递计算值,你能不能再写详情
    【解决方案3】:

    你可以试试这个,看看它会产生什么结果 公共函数 show($lon, $lat, $radio) {

    $results = Viaje::where($otherStuff)->select('fields','radio + '. $radio .'as total_radio')
                    ->whereRaw('total_radio <='. function($q) use ($lat,$lot,$this)  {
                        return $this->getDistanceFromLatLonInKm($lat, $lon,$q->select('lattitude'), $q->select('longitude'));
            });
    return response()->json($results);
     }
    

    【讨论】:

    • Laravel 不允许我放 $this,所以我放了 $radio,我一直有 errorException: Object of class Closure could not be converted to string。我试图理解这个错误但没有成功,有什么想法吗?
    • 从函数中删除参数 $this 并使这个 getDistanceFromLatLonInKm() 函数静态,然后使用它。它将解决您的问题
    • 它不起作用,同样的错误:S 奇怪的是它指向只有我拥有的行:});
    • public function show($lon, $lat, $radio) { $results = Viaje::where($otherStuff)->select('fields','radio + '.$radio .' as total_radio') ->whereRaw('total_radio select('latitude'), $q ->选择('经度')); });返回响应()->json($results); } errorException: 类 Closure 的对象无法转换为字符串
    猜你喜欢
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2017-09-27
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多