Laravel为DB :: raw查询启用分页

时间:2017-08-22 18:44:35

标签: mysql pagination laravel-5.4

我有以下原始查询,需要启用分页。我是否应该实施手动分页,还是可以使用构建分页方法?如果我可以使用内置的分页方法,请解释我怎么能完成它,因为我试了几次没有运气。

$sql = 'select t1.rownum, t2.rownum, t1.logged_on, t1.reading, (t1.reading - t2.reading) as diff
            from (
                select @rownum := @rownum + 1 AS rownum, date(t.logged_at) as logged_on, t.reading
                from parameter_log_tab t, (SELECT @rownum := 0) r
                where t.site_id = ?
                order by t.logged_at desc ) t1
            left join (
                select @rowno := @rowno + 1 AS rownum, date(t.logged_at) as logged_on, t.reading
                from parameter_log_tab t, (SELECT @rowno := 0) r
                where t.site_id = ?
                order by t.logged_at desc) t2
            on t1.rownum = t2.rownum - 1';
$result = DB::select($sql, [$site_id, $site_id]);

1 个答案:

答案 0 :(得分:0)

你必须自己创建分页。可能你看起来像这样。

$collection = new Collection($put_your_array_here);

// Paginate
$perPage = 10; // Item per page
$currentPage = Input::get('page') - 1; // url.com/test?page=2
$pagedData = $collection->slice($currentPage * $perPage, $perPage)->all();
$collection= Paginator::make($pagedData, count($collection), $perPage);

这里有一个堆栈溢出链接: Laravel 5 Pagination with Query Builder