laravel paginate raw sql 1

laravel paginate raw sql

$items = DB::table('team')   
    ->selectRaw('SELECT *,earth_distance(ll_to_earth(team.lat, team.lng), ll_to_earth(23.1215939329,113.3096030895)) AS distance')
    ->whereRaw('earth_box(ll_to_earth(23.1215939329,113.3096030895),1000) @> ll_to_earth(team.lat, team.lng)')
    ->paginate(10);

foreach($items as $item) {
    echo $item->distance;
}

Here is what the above code is Doing:
1. We’re selecting all the columns from the team table.
2. We’re also selecting the distance between the team’s location and the location we’re searching from.
3. We’re using the earth_box function to find all the teams within 1000 meters of the location we’re searching from.
4. We’re using the earth_distance function to calculate the distance between the team’s location and the location we’re searching from.
5. We’re using the ll_to_earth function to convert the latitude and longitude values to a point on the earth.
6. We’re using the @> operator to check if the team’s location is within the earth_box.
7. We’re using the paginate function to paginate the results.
8. We’re looping through the results and printing the distance.

Similar Posts