带有雄辩查询的 Laravel 分页

Posted

技术标签:

【中文标题】带有雄辩查询的 Laravel 分页【英文标题】:Laravel Pagination with eloquent Queries 【发布时间】:2015-01-02 14:12:27 【问题描述】:

我正在尝试对上面有表格的模板进行分页。

这是我在具有雄辩查询的控制器中的功能。

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

我想在我的模板中使用分页。所以我会这样做。

    @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

但是thins 不起作用。我哪里错了?

提前致谢

【问题讨论】:

【参考方案1】:

如果你想使用分页而不是get 方法,你需要使用paginate 方法。

例如,工作分页应该是这样的:

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

现在在 Blade 模板中,您可以在 foreach 循环中显示用户并添加分页链接:

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

 $users->links() 

你也应该看看Pagination documentation

【讨论】:

你能否为我的雄辩的查询展示一下,我们需要怎么做? @CoolD 看看这个:***.com/questions/26383071/laravel-sortby-paginate 嘿@Marcin。我在 SO 上发布了另一个问题。可以看看吗? ***.com/questions/26777898/… 嘿@marcin,我已经在上面的链接上发布了问题

以上是关于带有雄辩查询的 Laravel 分页的主要内容,如果未能解决你的问题,请参考以下文章

带有“LIKE”语句的雄辩查询在 Laravel 6 中不起作用

关系数据库中的 Laravel 雄辩分页调用

Laravel Livewire 无法解析模板上分页的雄辩关系

Laravel - 真的很难理解雄辩

Laravel 雄辩的极限查询

laravel 雄辩的关系查询