2016-08-18 135 views
0

如何将参数传递给使用whereRaw的查询中的方法?将参数传递给使用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); 
} 

编辑:

“无线电”,“经度”和“纬度”是含有一个浮子列名值。

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; 
} 
+0

哪种方法? – jaysingkar

+0

getDistanceFromLatLonInKm($ lat1,$ lon1,$ lat2,$ lon2) – lulu666

+0

$ results = Viaje :: where($ otherStuff) - > whereRaw('radio +'。$ radio。'<='。$ this-> getDistanceFromLatLonInKm( $ lat,$ lon,'latitude','longitude')); – jaysingkar

回答

0

试试这个:

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); 
} 
0

我假设无线电在此lattitude和经度列在那里,如果不是这样比在地方电台和表名替换无线电表名.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); 
} 
+0

我刚刚编辑过我的帖子,说'radio','longitude'和'latitude'是列名。表名是Viaje。 – lulu666

+0

和Viaje.latitude不起作用,给我错误500. – lulu666

+0

什么是无线电和你在$ radio中传递什么值假设无线电是你应用的列名,你可以在哪里添加相关表格模式这样我就可以为它写下查询 –

0

你可以试试这个,看看它产生的结果 公共职能秀($ 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); 
} 
+0

Laravel不允许我放置$ this,因此我放置$ radio,我试图理解这个错误没有成功,任何想法? – lulu666

+0

从函数中删除参数$ this,并使此getDistanceFromLatLonInKm()函数为静态,并使用它。将解决您的问题 –

+0

它不起作用,同样的错误:S而奇怪的是它指向只有我有︰}); – lulu666