Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel5中使用原始sql分页?_Php_Laravel_Laravel 5_Pagination - Fatal编程技术网

Php 如何在Laravel5中使用原始sql分页?

Php 如何在Laravel5中使用原始sql分页?,php,laravel,laravel-5,pagination,Php,Laravel,Laravel 5,Pagination,这是我的控制器代码: $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); "; $re

这是我的控制器代码:

$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


iOS或android将发送下一页参数,如何使用它并查找下一节数据

据我所知,您无法为原始查询分页,原因如下:

$result = DB::select($sql); 
此处的$result将具有数组类型,paginate是来自illumb\Database\Query\Builder类的方法

您的案例可以通过以下方式执行:

$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方法所需的工作量很小。

据我所知,您无法为原始查询分页,原因如下:

$result = DB::select($sql); 
此处的$result将具有数组类型,paginate是来自illumb\Database\Query\Builder类的方法

您的案例可以通过以下方式执行:

$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;
}

正如您所看到的,在这里分离原始查询以选择raw和WHERRAW方法所需的工作量很小。

如果您试图对动态列进行分页,则另一个选项是创建排序方法并传入数组和参数:

public function sort($array_of_objects, $sort_by=null, $order, $page) 
{
    $collection = collect($array_of_objects);
    if ($sort_by)
    {

        if ($order=='desc') {
            $sorted = $collection->sortBy(function($role) use ($sort_by)
            {
                return $role->{$sort_by};
            })->reverse();
        } else if ($order=='asc') {
            $sorted = $collection->sortBy(function($role) use ($sort_by)
            {
                return $role->{$sort_by};
            });
        }
    } else {
        $sorted = $collection;
    }

    $num_per_page = 20;
    if (!$page) {
        $page = 1;
    }

    $offset = ( $page - 1) * $num_per_page;
    $sorted = $sorted->splice($offset, $num_per_page);

    return  new Paginator($sorted, count($array_of_objects), $num_per_page, $page);

}

如果您试图对可能正在处理报告计算的动态列进行分页,另一个选项是创建排序方法并传入数组和参数:

public function sort($array_of_objects, $sort_by=null, $order, $page) 
{
    $collection = collect($array_of_objects);
    if ($sort_by)
    {

        if ($order=='desc') {
            $sorted = $collection->sortBy(function($role) use ($sort_by)
            {
                return $role->{$sort_by};
            })->reverse();
        } else if ($order=='asc') {
            $sorted = $collection->sortBy(function($role) use ($sort_by)
            {
                return $role->{$sort_by};
            });
        }
    } else {
        $sorted = $collection;
    }

    $num_per_page = 20;
    if (!$page) {
        $page = 1;
    }

    $offset = ( $page - 1) * $num_per_page;
    $sorted = $sorted->splice($offset, $num_per_page);

    return  new Paginator($sorted, count($array_of_objects), $num_per_page, $page);

}

@肯尼:如果有帮助,你可以接受我的回答给其他读者:@Kenny如果有帮助,你可以接受我的回答给其他读者: