Skip to content

Instantly share code, notes, and snippets.

@thanhthai3010
Forked from wuiler/paginate.php
Created June 29, 2018 07:19
Show Gist options
  • Save thanhthai3010/1504820e4e16dff04c01a1b0f3f016d2 to your computer and use it in GitHub Desktop.
Save thanhthai3010/1504820e4e16dff04c01a1b0f3f016d2 to your computer and use it in GitHub Desktop.
Laravel Paginate Collection or Array
<?php
/**
* Gera a paginação dos itens de um array ou collection.
*
* @param array|Collection $items
* @param int $perPage
* @param int $page
* @param array $options
*
* @return LengthAwarePaginator
*/
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
@thanhthai3010
Copy link
Author

`<?php
/**

  • I solved the problem with the "page>2" which add numeric key to the object.

  • Gera a paginação dos itens de um array ou collection.

  • @param array|Collection $items

  • @param int $perPage

  • @param int $page

  • @param array $options

  • @return LengthAwarePaginator
    */
    public function paginateWithoutKey($items, $perPage = 15, $page = null, $options = [])
    {

    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    
    $items = $items instanceof Collection ? $items : Collection::make($items);
    
    $lap = new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    
    return [
        'current_page' => $lap->currentPage(),
        'data' => $lap ->values(),
        'first_page_url' => $lap ->url(1),
        'from' => $lap->firstItem(),
        'last_page' => $lap->lastPage(),
        'last_page_url' => $lap->url($lap->lastPage()),
        'next_page_url' => $lap->nextPageUrl(),
        'per_page' => $lap->perPage(),
        'prev_page_url' => $lap->previousPageUrl(),
        'to' => $lap->lastItem(),
        'total' => $lap->total(),
    ];
    

    }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment