I ran into an issue earlier where I needed to use the 'paginate()' function on a collection, just to discover that you can't actually do that on a collection!

No problem though, collections are macroable so I can make it paginate. Just drop this into your AppServiceProvider.php files 'boot()' method and away you go!

Collection::macro('paginate', function(int $perPage = 15, $page = null, $options = []) {
    /** @var Collection $this */
    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    return new LengthAwarePaginator(
        $this->forPage($page, $perPage)->values(),
        $this->count(),
        $perPage,
        $page,
        $options
    );
});

Now you can use the paginate function just like you would in a query builder.

collect(range(1,15))
  ->paginate(5, 2, [
    'path' => 'http://example.com/foo'
  ])
  ->toArray();

/*
[
    "current_page" => 2,
    "data" => [
        6,
        7,
        8,
        9,
        10,
    ],
    "first_page_url" => "http://example.com/foo?page=1",
    "from" => 6,
    "last_page" => 3,
    "last_page_url" => "http://example.com/foo?page=3",
    "next_page_url" => "http://example.com/foo?page=3",
    "path" => "http://example.com/foo",
    "per_page" => 5,
    "prev_page_url" => "http://example.com/foo?page=1",
    "to" => 10,
    "total" => 15,
]
*/