I have a tricky case ...
Following database query does not work:
DB::table(\'posts\')
->select(\'posts.*\', DB::raw($haversineSQL . \' as distance
You can calculate the distance in the WHERE part:
DB::table('posts')
->whereRaw($haversineSQL . '<= ?', [$distance])
->paginate(10);
If you need the distance value in your application, you'll have to calculate it twice:
DB::table('posts')
->select('posts.*', DB::raw($haversineSQL . ' as distance'))
->whereRaw($haversineSQL . '<= ?', [$distance])
->paginate(10);