Laravel Pagination with eloquent Queries

Jay Desai picture Jay Desai · Nov 6, 2014 · Viewed 20.8k times · Source

I am trying to paginate my template which has a table on it.

This is my function in the controller which has the eloquent queries.

public function orderbydate()
    {

        $order =DB::table('sales_flat_order_items as s')
                ->leftJoin('sales_flat_orders as w', 'w.entity_id','=','s.order_id')
                ->select(array(DB::Raw('sum(s.amount_refunded) as amount_refunded'),
                    DB::Raw('sum(s.row_total) as row_total'),
                    DB::Raw('sum(s.discount_amount) as discount_amount'),
                    DB::Raw('sum(s.tax_amount) as tax_amount'),
                    DB::Raw('sum(s.qty_ordered) as qty_ordered'),
                    DB::Raw('sum(w.subtotal) as subtotal'), 
                    DB::Raw('sum(w.total_invoiced) as total_invoiced'), 
                    DB::Raw('sum(w.shipping_amount) as shipping_amount')))
                ->where('qty_canceled','=','0')
                ->where('status','!=','canceled')
                ->get();

        $orderbydate = DB::table('sales_flat_order_items as s')
                ->leftJoin('sales_flat_orders as w', 'w.entity_id','=','s.order_id')
                ->select(array(DB::Raw('sum(s.amount_refunded) as amount_refunded'),
                    DB::Raw('sum(s.row_total) as row_total'),
                    DB::Raw('sum(s.discount_amount) as discount_amount'),
                    DB::Raw('sum(s.tax_amount) as tax_amount'),  
                    DB::Raw('sum(s.qty_ordered) as qty_ordered'),
                    DB::Raw('sum(w.subtotal) as subtotal'),
                    DB::Raw('DATE(w.created_at) days'), 
                    DB::Raw('sum(w.total_invoiced) as total_invoiced'),
                    DB::Raw('sum(w.shipping_amount) as shipping_amount')))
                ->where('qty_canceled','=','0')
                ->where('status','!=','canceled')
                ->groupBy('days')
                ->orderBy('s.created_at')
                ->get();

            return View::make('sales_flat_orders.orderbydate', compact('order','orderbydate'), ['orderbydate'=>Paginate(3)]);
    }

I want to use pagination in my template. So i ma doing like this.

    @foreach(array_chunk($orderbydate->getCollection()->all(), 3) as $row)
      <div class = "row">
          @foreach($row as $s)
          <article class = "col-md-4">
     <tbody>
      <tr class="odd gradeX">
         <td>{{date("d F, Y",strtotime($s->days))}}</td>
         <td>{{ round($s->qty_ordered, 2) }}</td>
         <td>{{round($s->subtotal,2)}}</td>
         <td>{{round($s->tax_amount,2)}}</td>
         <td>{{round($s->discount_amount,2)}}</td>
         <td>{{round($s->row_total,2)}}</td>
         <td>{{round($s->amount_refunded,2)}}</td>
         <td>{{round($s->total_invoiced,2)}}</td>
         <td>{{round($s->shipping_amount,2)}}</td>
      </tr>
    </article>
     @endforeach
    @endforeach

But thins does not work. Where am i going wrong?

Thanks in advance

Answer

Marcin Nabiałek picture Marcin Nabiałek · Nov 6, 2014

If you want to use pagination instead of get method you need to use paginate method.

For example the working pagination should work like this:

Paginator::setCurrentPage($page);
Paginator::setBaseUrl($baseUrl);
Paginator::setPageName('page');
$users = User::paginate(15);

now in Blade template you can display users in foreach loop and add pagination links:

@foreach ($users as $user)
  {{{ $user->name }}}
@endforeach

{{ $users->links() }}

You should definitely look also at Pagination documentation