【问题标题】:Pagination output laravel blade分页输出 laravel 刀片
【发布时间】:2020-11-27 22:40:40
【问题描述】:

我是 Laravel 的初学者。

我需要用数据对表格进行分页。无法理解如何设置链接。我正在尝试搜索一些文档,但不明白我该怎么做。

我的控制器:

public function index()
    {
        $users = Viewers::all()->forPage(1, 5)->sortByDesc('last_activity');
        $users->setPath('/admin');
        return view('pages.admin.dashboard', ['users'=>$users]);
    }

我的dashboard.blade.php

@extends('layouts.admin')

@section('content')
    <div class="content">
        <table class="table">
            <thead>
            <tr>
                <th class="text-center">#</th>
                <th>IP</th>
                <th>Request URI</th>
                <th>Country</th>
                <th>City</th>
                <th>Device</th>
                <th>Last Activity</th>
            </tr>
            </thead>
            @foreach($users as $user)
                <tbody>

                <tr>
                    <td class="text-center">{{$user->id}}</td>
                    <td>{{$user->ip_address}}</td>
                    <td>{{$user->request_uri}}</td>
                    <td>{{$user->country}}</td>
                    <td>{{$user->city}}</td>
                    <td>{{$user->device}}</td>
                    <td>{{$user->last_activity}}</td>
                </tr>
                </tbody>
            @endforeach
        </table>
        <nav aria-label="Page navigation example">
            <ul class="pagination">
                <li class="page-item"><a class="page-link" href="#">Previous</a></li>
                <li class="page-item"><a class="page-link" href="#">1</a></li>
                <li class="page-item"><a class="page-link" href="#">2</a></li>
                <li class="page-item"><a class="page-link" href="#">3</a></li>
                <li class="page-item"><a class="page-link" href="#">Next</a></li>
            </ul>
        </nav>
    </div>
@endsection

【问题讨论】:

  • 请查看分页文档:laravel.com/docs/8.x/pagination。在您看来,这应该像$users = Users::orderBy('last_activity', 'DESC')-&gt;paginate(100); 一样简单,然后是$users-&gt;links()。旁注,不要使用User::all(),除非你想要所有用户。与数据库排序相比,将它们加载到内存中进行排序是低效的。
  • 将此行更改为$users = Viewers::sortByDesc('last_activity')-&gt;paginate(10); 现在只需将此代码{{ $users-&gt;links() }} 放在foreach 循环下
  • @sta 'Call to undefined method App/Viewers::sortByDesc()'。你误会了BuilderCollection 方法。模型查询,在通过闭包之前是Builder实例,没有sortBy()sortByDesc(),需要使用orderBy()Collections 使用 sortBy()。请查看并更正您的评论。 laravel.com/api/8.x/Illuminate/Database/Query/…laravel.com/api/8.x/Illuminate/Support/…laravel.com/api/8.x/Illuminate/Support/…
  • @Tim Lewis 我的愚蠢错误
  • @TimLewis 添加答案 - 我会接受的

标签: php laravel pagination


【解决方案1】:

Viewers::all() 将数据库中的每条记录加载到Collection 中。集合具有sortBy()sortByDesc() 方法,但模型(查询时为Builder 实例)具有orderBy() 方法。在大多数情况下,使用 DB 排序会比 PHP/Laravel 排序更有效,所以除非需要,否则尽量不要使用::all()

话虽如此,您的查询可以固定为:

$users = Viewers::orderBy('last_activity', 'DESC')->paginate(100); // Replace 100 with desired number per page

然后,在您的 .blade.php 文件中,使用 Pagination 实例上可用的 links() 方法:

{{ $users->links() }}

这将根据每页的记录数输出 First、Previous、Page(s)、Next 和 Last 链接。

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 2014-08-14
    • 2016-06-08
    • 2022-09-29
    • 2019-05-16
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多