Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Pagination with Query Builder

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

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

Thanks

like image 395
jackhammer013 Avatar asked Jun 16 '15 07:06

jackhammer013


1 Answers

in the doc at this page: http://laravel.com/docs/5.1/pagination we can see that we are not forced to use eloquent.

$users = DB::table('users')->paginate(15);

but, be sure you don't make a groupBy in your query because, the paginate method uses it.

after i'm no sure you can use paginate with query builder ( select($query) )

--- edit

You can create collection an use the paginator class :

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

and in your view just use $collection->render();

like image 174
Bouhnosaure Avatar answered Sep 30 '22 20:09

Bouhnosaure