Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
@wuiler

This comment has been minimized.

Copy link
Owner Author

wuiler commented Jan 16, 2018

<?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(),
        ];
    }
@fatihgune

This comment has been minimized.

Copy link

fatihgune commented Apr 19, 2018

@vuiler Thank you.

@MarcoSantana

This comment has been minimized.

Copy link

MarcoSantana commented Feb 25, 2019

Is there a recomendend place to put this?

@vishwapriyanatha

This comment has been minimized.

Copy link

vishwapriyanatha commented Mar 13, 2019

Thanks

@aeadedoyin

This comment has been minimized.

Copy link

aeadedoyin commented May 23, 2019

Thanks!

@aeadedoyin

This comment has been minimized.

Copy link

aeadedoyin commented May 23, 2019

Is there a recomendend place to put this?

@MarcoSantana
I created a 'Utils/Utils.php' in the 'App' folder and kept every repeatedly used special function there.

@freelance-github

This comment has been minimized.

Copy link

freelance-github commented Jul 26, 2019

I solve it can access it from collect() function like collect([2=>'some think', 9 => 'Hi'])
so go to AppServiceProvider.php and past this in boot function

/** * Bootstrap any application services. * * @return void */ public function boot() { /** * Paginate a standard Laravel Collection. * * @param int $perPage * @param int $total * @param int $page * @param string $pageName * @return array */ Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') { $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName); return new LengthAwarePaginator( $this->forPage($page, $perPage)->values(), $total ?: $this->count(), $perPage, $page, [ 'path' => LengthAwarePaginator::resolveCurrentPath(), 'pageName' => $pageName, ] ); }); }

@freelance-github

This comment has been minimized.

Copy link

freelance-github commented Jul 26, 2019

I solve it

collect( [ 2 => 'Some value', 5 => 'other value' ] )->paginate(15)

https://gist.github.com/freelance-github/fe8488ff19b1a7ed67223b15c7a25b52

@HieuMinh19

This comment has been minimized.

Copy link

HieuMinh19 commented Dec 2, 2019

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.