How to use pagination in laravel 5 with Raw query

后端 未结 2 383
温柔的废话
温柔的废话 2020-12-11 04:46

I have a simple question and I didn´t find what I need.

I need to calculate the distance between t2 geocode points for a list of Stores. I also need it to be paginat

2条回答
  •  [愿得一人]
    2020-12-11 05:21

    Use the selectRaw method on the Eloquent model.

    Store::selectRaw('*, distance(lat, ?, lng, ?) as distance', [$lat, $lon])
        ->orderBy('distance')
        ->paginate(10);
    

    In that case Laravel asks the database for the amount of rows (using select count(*) as aggregate from stores) which saves your RAM.

提交回复
热议问题