Eru*_*o87 6 php pagination laravel laravel-5
我有一个简单的问题,我找不到我需要的东西.
我需要计算商店列表的t2地理编码点之间的距离.我还需要将它分页为WebService.
这有效,但结果中没有距离:
public function stores(){
return Store::paginate(10);
}
Run Code Online (Sandbox Code Playgroud)
结果是:
{
total: 4661,
per_page: 10,
current_page: 6,
last_page: 467,
next_page_url: "WS_URL/stores/?page=7",
prev_page_url: "WS_URL/stores/?page=5", from: 51,
to: 60,
data: [ {
id: "51",
name: "Sprouts",
.
.
lng: "-118.359688",
lat: "33.808281",
country: "usa"
},
.
.
.
]}
Run Code Online (Sandbox Code Playgroud)
但我需要这个代码工作:
public function stores(){
return DB::table('stores')
->selectRaw(' *, distance(lat, ?, lng, ?) as distance ')
->setBindings([ 41.123401,1.2409893])
->orderBy('distance')
->paginate($this->limit);
}
Run Code Online (Sandbox Code Playgroud)
这就是结果:
{total: 0,
per_page: 10,
current_page: 1,
last_page: 0,
next_page_url: null,
prev_page_url: null,
from: 1,
to: 10,
data: [{
id: "3686",
name: "Bon Area",
.
.
lng: "1.602016",
lat: "41.266823",
distance: "0.15091"
},
.
.
.
]
}
Run Code Online (Sandbox Code Playgroud)
我需要next_page_url
和prev_page_url
有任何想法吗?
selectRaw
在Eloquent模型上使用该方法.
Store::selectRaw('*, distance(lat, ?, lng, ?) as distance', [$lat, $lon])
->orderBy('distance')
->paginate(10);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Laravel会向数据库询问select count(*) as aggregate from stores
保存RAM 的行数(使用 ).