How to use raw sql Pagination in Laravel5?(如何在 Laravel5 中使用原始 sql 分页?)
问题描述
这是我的控制器代码:
$sql = "SELECT *,earth_distance(ll_to_earth(team.lat, team.lng), ll_to_earth(23.1215939329,113.3096030895)) AS distance FROM team where earth_box(ll_to_earth(23.1215939329,113.3096030895),1000) @> ll_to_earth(team.lat, team.lng); ";
$result = DB::select( DB::raw( $sql ) );
如何向此代码添加分页以构建我的 Restful api?
How can I add pagination to this code to build my restful api?
iOS 或 android 会发送next page"参数,如何使用和查找下一段数据?
iOS or android will send the "next page" parameter, how to use it and find the next section data?
推荐答案
据我所知,您无法对原始查询进行分页,原因如下:
As far as I know you can't paginate raw query, here's why:
$result = DB::select($sql);
$result 这里有数组类型,paginate() 是来自 IlluminateDatabaseQueryBuilder 类的方法.
$result here will have the array type and paginate() is the method from the IlluminateDatabaseQueryBuilder class.
您的案例可以这样执行:
Your case can be performed this way:
$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;
}
如您所见,将原始查询分离到 selectRaw() 和 whereRaw() 方法所需的工作量很小.
As you can see minimal effort is needed here to separate raw query to selectRaw() and whereRaw() methods.
这篇关于如何在 Laravel5 中使用原始 sql 分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Laravel5 中使用原始 sql 分页?
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
