Laravel 5使用查询生成器进行分页 [英] Laravel 5 Pagination with Query Builder

查看:85
本文介绍了Laravel 5使用查询生成器进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据查询结果进行Laravel分页,并在我的视图中进行渲染.我正在遵循本指南 http://laravel.com/docs/5.1/pagination ,但我出现错误:

I'm making a Laravel Pagination based from my query result and be rendered in my view. I'm following this guide http://laravel.com/docs/5.1/pagination but I get an error:

Call to a member function paginate() on a non-object

我正在使用查询生成器,所以我认为应该没问题吗?这是我的代码

I'm using query builder so I think that should be ok? Here's my code

public function getDeliveries($date_from, $date_to)
{
    $query = "Select order_confirmation.oc_number as oc,
    order_confirmation.count as cnt,
    order_confirmation.status as stat,
    order_confirmation.po_number as pon,
    order_summary.date_delivered as dd,
    order_summary.delivery_quantity as dq,
    order_summary.is_invoiced as iin,
    order_summary.filename as fn,
    order_summary.invoice_number as inum,
    order_summary.oc_idfk as ocidfk,
    order_summary.date_invoiced as di
    FROM
    order_confirmation,order_summary
    where order_confirmation.id = order_summary.oc_idfk";

    if (isset($date_from)) {
        if (!empty($date_from))
        {
            $query .= " and order_summary.date_delivered >= '".$date_from."'";
        }
    }

    if (isset($date_to)) {
        if (!empty($date_to)) 
        {
            $query .= " and order_summary.date_delivered <= '".$date_to."'";
        }
    }

    $query.="order by order_confirmation.id ASC";

    $data = DB::connection('qds106')->select($query)->paginate(15);
    return $data;
}

但是,当我去除分页纸时(15);效果很好.

However when I remove the paginate(15); it works fine.

谢谢

推荐答案

public function getDeliveries($date_from, $date_to)
{
    $query="your_query_here";
    $deliveries = DB::select($query);

    $deliveries = collect($deliveries);
    $perPage = 10;
    $currentPage = \Input::get('page') ?: 1;
    $slice_init = ($currentPage == 1) ? 0 : (($currentPage*$perPage)-$perPage);
    $pagedData = $users->slice($slice_init, $perPage)->all();
    $deliveries = new LengthAwarePaginator($pagedData, count($deliveries), $perPage, $currentPage);
    $deliveries ->setPath('set_your_link_page');
    return $deliveries;
 }

这篇关于Laravel 5使用查询生成器进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆