How to paginate an Laravel 7 array?

Asked

Viewed 68 times

1

Is there any way simple to paginate an Array in Laravel?

I have an array $calls what date a search makes the return of type:

inserir a descrição da imagem aqui

Until version 5 there was the method

Paginator::make($atendimentos, count($atendimentos), 10);

In which you passed the array, its size and the registration number you wanted per page. However it was removed and I researched a lot, but I found nothing in the new versions.

Edited: I am using in the system Lengthawarepaginator in the rest of the system and I imagine necessary in this case as well, because my $call array comes from a search. But if there is any way with the Paginator accept suggestions.

  • https://laravel.com/docs/8.x/pagination#Manually-Creating-a-Paginator does have yes

  • 1

    @novic reopened, feel free to formalize an answer, despite believing that the question needs clarification, as there are cases that will have to be Illuminate\Pagination\Paginator and others Illuminate\Pagination\LengthAwarePaginator

1 answer

1


In the documentation of Laravel, yet Paginator as mentioned in your question, in case the edition was specified LengthAwarePaginator, then create an instance following the whole namespace which is described in the documentation:

Caveats:

  • 1 - the variable $atendimentos should only come the total of items that will be presented in your View (can have filters and sorting);
  • 2 - the variable $atendimentosTotal is the general amount of records (may or may not have filters and is well related to item 1, only it differs because here is the total of record and in item 1 is the part that will be presented usually demonstrated by the current page of item 4);
  • 3 - the variable $atendimentosTotalPorPagina is the count($atendimentos) and;
  • 4 - the $page variable is the number of the current page.

Code:

$page = new \Illuminate\Pagination\LengthAwarePaginator(
    $atendimentos, 
    $atendimentosTotal, 
    $atendimentosTotalPorPagina
    $pagina
);

As it is manual you have to calculate these variables before passing this class so that it organizes all the information and presents the data correctly.

References

  • Is it possible to illustrate your variable attendance? has to ask the question?

  • https://www.php.net/manual/en/function.array-slice.php @PauloMartins

  • @Paulomartins because you don’t do straight in builder of his Eloquent?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.