mercredi 3 août 2016

Calling method in whereRaw using column names as parameters

I need to make a query calling the following method:

public function getDistanceFromLatLonInKm($lat1,$lon1,$lat2,$lon2) {
  ...
  $d = R * c; // Distance in km
  return $d;
}

I've got 'syntax error, unexpected '.'' when trying to add 'latitude' and 'longitude' as parameters:

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

And on this way I got error 500:

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

How can I make that query passing $lat, $lon as variables and 'latitude', 'longitude' as values for every register.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire