I want to paginate a shuffled array of elements. I know Laravel Paginator expect a Collection. In more details, I want to shuffle a resultset from the database. Therefore I have the following line of code:
$getallFragen = $frage->getAllFragen2()->shuffle()->all();
This line selects all questions and shuffles the collection.
Furthermore I want to shuffle the answers from the questions, which also works correctly. Then I want to paginate the items with one item per page, which also works properly, but I receive duplicate items in the pagination. First I thought, it has to be the collection, but the collection is built correctly and I don´t know, what the problem could be. I could imagine, that my own paginator is not working the way it should. I also create an array, where I store the items, which are allowed in the exam and then make a collection out of the array to make sure, that there are no duplicate entries.
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$itemCollection = collect($pruefung);
$perPage = 1;
$currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();
$paginatedItems = new LengthAwarePaginator($currentPageItems, count($itemCollection), $perPage);
$paginatedItems->setPath($request->url());
return view('pruefungssimulation', ["fragen" => $paginatedItems]);
In the frontend I just use @foreach($fragen as $frage)
to get all items out of the collection.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2JCy96R
via IFTTT
Aucun commentaire:
Enregistrer un commentaire