Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分页输出laravel刀片_Php_Laravel_Pagination - Fatal编程技术网

Php 分页输出laravel刀片

Php 分页输出laravel刀片,php,laravel,pagination,Php,Laravel,Pagination,我是拉威尔的初学者 我需要用数据给我的表分页。无法理解如何设置链接。我试图搜索一些文档,但不明白如何才能做到这一点 我的控制器: public function index() { $users = Viewers::all()->forPage(1, 5)->sortByDesc('last_activity'); $users->setPath('/admin'); return view('pages.admin.d

我是拉威尔的初学者

我需要用数据给我的表分页。无法理解如何设置链接。我试图搜索一些文档,但不明白如何才能做到这一点

我的控制器:

public function index()
    {
        $users = Viewers::all()->forPage(1, 5)->sortByDesc('last_activity');
        $users->setPath('/admin');
        return view('pages.admin.dashboard', ['users'=>$users]);
    }
My
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
@extends('layouts.admin'))
@节(“内容”)
#
知识产权
请求URI
国家
城市
装置
最后一项活动
@foreach($users作为$user)
{{$user->id}
{{$user->ip_地址}
{{$user->request_uri}
{{$user->country}
{{$user->city}
{{$user->device}
{{$user->last_activity}
@endforeach
@端部
Viewers::all()
将数据库中的每条记录加载到
集合中。集合具有
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
文件中,使用分页实例上可用的
links()
方法:

{{ $users->links() }}

这将根据每页记录的数量输出第一个、上一个、第页、下一个和最后一个链接。

请查看有关分页的文档:。这应该像
$users=users::orderBy('last_activity','DESC')->paginate(100)一样简单
,然后在视图中选择
$users->links()
。旁注,不要使用
User::all()
,除非您需要所有用户。与数据库排序相比,将它们加载到内存中进行排序效率较低。将此行更改为
$users=Viewers::sortByDesc('last_activity')->paginate(10)
现在只需将此代码
{{{$users->links()}}
放在foreach下面loop@sta
“调用未定义的方法App/Viewers::sortByDesc()”
。你把
Builder
Collection
方法搞错了。模型查询在传递闭包之前都是
Builder
实例,没有
sortBy()
sortByDesc()
,您需要使用
orderBy()
<代码>集合
s使用
sortBy()
。请检查并更正您的评论,蒂姆·刘易斯是我的傻瓜mistake@TimLewis添加答案-我会接受的