2016-08-18 134 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