问题
I'm using the haversine formula to calculate a distance, this is working fine.
But I would like to hide results where the distance is greater then the max_radius field on that result.
This is my database scheme.
This is the query I'm using. You can see I hard coded the distance (50)
public function scopeFitsDistance($query, $lat, $lng)
{
return $query->select(\DB::raw("*,
( 3959 * acos( cos( radians(?) ) *
cos( radians( lat ) )
* cos( radians( lng ) - radians(?)
) + sin( radians(?) ) *
sin( radians( lat ) ) )
) AS distance"))
->addBinding($lat, 'select')
->addBinding($lng, 'select')
->addBinding($lat, 'select')
->having('distance', '<', 50); <----------
}
But now I'm wondering how I can hide results where that distance < max_radius, which is a field inside the table.
The following returns no results
->having('distance', '<', 'max_radius');
Thank you!
回答1:
HAVING
only works with GROUP BY
You can do a sub-select and then use a WHERE
clause instead.
来源:https://stackoverflow.com/questions/42438137/laravel-own-variable-in-scope