Laravel 5分页查询生成器

jac*_*013 6 php pagination laravel laravel-5

我根据查询结果制作了一个Laravel分页,并在我的视图中呈现.我正在遵循本指南http://laravel.com/docs/5.1/pagination但我收到错误:

Call to a member function paginate() on a non-object
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我删除分页(15); 它工作正常.

谢谢

小智 4

在此页面的文档中:http://laravel.com/docs/5.1/pagination 我们可以看到我们并没有被迫使用 eloquent。

$users = DB::table('users')->paginate(15);
Run Code Online (Sandbox Code Playgroud)

但是,请确保您不在查询中创建 groupBy,因为分页方法使用它。

在我不确定您是否可以将分页与查询生成器一起使用之后(select($query)

- - 编辑

您可以创建集合并使用分页器类:

$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);
Run Code Online (Sandbox Code Playgroud)

在你看来只需使用$collection->render();