Ad

Laravel 5 How To Use Pagination With Files List?

I have one problem with my script. I did foreach with files list but I need pagination, for example 15 files per site. What should I do now? Thanks for help :)

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 target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" 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>

I tried use paginate() but this option not working :(

Ad

Answer

What you can do is:

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

And for displaying in view you can use https://laravel.com/docs/5.1/pagination#displaying-results-in-a-view

Ad
source: stackoverflow.com
Ad