I am calculating the distance of a teacher from a school.
I am querying the user table and then the userable_type teacher. The teacher table has the longitude and latitude values.
The following worked for me elsewhere when I query the teacher model directly but not when querying through the user model first!
$query->whereHas('teacher', function ($query) use ($school) {
$haversine = "(3961 * acos(cos(radians($school->latitude))
* cos(radians(latitude))
* cos(radians(longitude)
- radians($school->longitude))
+ sin(radians($school->latitude))
* sin(radians(latitude))))";
$query->select() //pick the columns you want here.
->selectRaw("{$haversine} AS distance")
->whereRaw("{$haversine} < ?", '20');
// ->havingRaw('{$haversine}', '<', '20');
});
The 'distance' field is not created but no errors are thrown! So my question is, in this scenario, how do I add the 'distance' value to each user as I would like to order by the distance?
Thanks, KH...
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire