[英]Pagination output laravel blade
I'm a beginner in Laravel.我是 Laravel 的初学者。
I need Paginate my Table with Data.我需要用数据对我的表进行分页。 Can't understand how setup links.无法理解如何设置链接。 I'm trying to search some documentation but don't understand how I can do this.我正在尝试搜索一些文档,但不明白我该怎么做。
My Controller:我的 Controller:
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
:我的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
Viewers::all()
loads every record in your database into a Collection
. Viewers::all()
将数据库中的每条记录加载到Collection
中。 Collections have sortBy()
and sortByDesc()
methods, but Models, which are Builder
instances while querying, have an orderBy()
method. Collections 有sortBy()
和sortByDesc()
方法,但是查询时作为Builder
实例的模型有orderBy()
方法。 Using DB ordering will in most cases be more efficient than PHP/Laravel sorting, so try not to use ::all()
unless you need to.在大多数情况下,使用 DB 排序比 PHP/Laravel 排序更有效,所以除非需要,否则尽量不要使用::all()
。
With that being said, your query can be fixed to:话虽如此,您的查询可以固定为:
$users = Viewers::orderBy('last_activity', 'DESC')->paginate(100); // Replace 100 with desired number per page
Then, in your .blade.php
file, use the links()
method available on Pagination instances:然后,在您的.blade.php
文件中,使用 Pagination 实例上可用的links()
方法:
{{ $users->links() }}
That will output First, Previous, Page(s), Next and Last links, based on the number of records per page.这将 output 第一个、上一个、页面、下一个和最后一个链接,基于每页的记录数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.