我有一个简单的问题,但没有找到我需要的.
I have a simple question and I didn´t find what I need.
我需要计算商店列表的 t2 地理编码点之间的距离.我还需要为 WebService 分页.
I need to calculate the distance between t2 geocode points for a list of Stores. I also need it to be paginated for a WebService.
这可行,但结果中没有距离:
This works, but there is no distance in the result:
public function stores(){
return Store::paginate(10);
}
结果是:
{
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"
},
.
.
.
]}
但我需要这段代码工作:
But I need this code working:
public function stores(){
return DB::table('stores')
->selectRaw(' *, distance(lat, ?, lng, ?) as distance ')
->setBindings([ 41.123401,1.2409893])
->orderBy('distance')
->paginate($this->limit);
}
这是结果:
{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"
},
.
.
.
]
}
我需要 next_page_url
和 prev_page_url
有什么想法吗?
在 Eloquent 模型上使用 selectRaw
方法.
Use the selectRaw
method on the Eloquent model.
Store::selectRaw('*, distance(lat, ?, lng, ?) as distance', [$lat, $lon])
->orderBy('distance')
->paginate(10);
在这种情况下,Laravel 会向数据库询问行的数量(使用 select count(*) as aggregate from stores
)以节省您的 RAM.
In that case Laravel asks the database for the amount of rows (using select count(*) as aggregate from stores
) which saves your RAM.
这篇关于如何在带有原始查询的 Laravel 5 中使用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!