gpt4 book ai didi

php - Laravel 5 如何使用文件列表分页?

转载 作者:可可西里 更新时间:2023-11-01 00:58:26 28 4
gpt4 key购买 nike

我的脚本有一个问题。我用文件列表做了 foreach 但我需要分页,例如每个站点 15 个文件。我现在应该怎么办?感谢您的帮助:)

Controller :

$files = Storage::allFiles('/upload_file');

return view('cms.viewSystem.gallery.manageFiles')->with('files', $files);

Blade View :

<table class="table table-bordered table-striped datatable" id="table-2">
<thead>
<tr>
<th>Nazwa pliku</th>
<th>Akcje</th>
</tr>
</thead>
<tbody>
@foreach($files as $file)
<tr>
<td>{{ $file }}</td>
<td>
<a href="{{ url('cms/media/deleteFile/'.$file) }}" class="btn btn-red btn-sm btn-icon icon-left"><i class="entypo-cancel"></i>Usuń</a>
</td>
</tr>
@endforeach
</tbody>
</table>

我试过使用 paginate() 但这个选项不起作用:(

最佳答案

你可以做的是:

$page = (int) $request->input('page') ?: 1;

$files = collect(Storage::allFiles('/upload_file'));
$onPage = 15;

$slice = $files->slice(($page-1)* $onPage, $onPage);

$paginator = new \Illuminate\Pagination\LengthAwarePaginator($slice, $files->count(), $onPage);
return view('cms.viewSystem.gallery.manageFiles')->with('files', $paginator);

为了在 View 中显示,您可以使用 https://laravel.com/docs/5.1/pagination#displaying-results-in-a-view

关于php - Laravel 5 如何使用文件列表分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581260/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com