Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shahrukh4's avatar

You can try this with your array, just replace the array name and hit...

        $page = isset($request->page) ? $request->page : 1; // Get the ?page=1 from the url

        $perPage = $pagination_num; // Number of items per page
        $offset = ($page * $perPage) - $perPage;

        $entries =  new LengthAwarePaginator(
            array_slice($ARRAY, $offset, $perPage, true),
            count($contact_list), // Total items
            $perPage, // Items per page
            $page, // Current page
            ['path' => $request->url(), 'query' => $request->query()] // We need this so we can 
            keep all old query parameters from the url
        );
Denisbeder's avatar

My solution.

I made a Trait where I step my collection to page:

protected function simplePaginate(Collection $datas, $perPage = 15)
{
        $currentPage = Paginator::resolveCurrentPage();
        $collect = $datas->forPage($currentPage, $perPage);

        // here is the magic
        $items = $currentPage > 1 ? $datas->slice($perPage * $currentPage - $perPage) : $datas;
        
        $paginate = new Paginator($items, $perPage, $currentPage, [
            'path' => Paginator::resolveCurrentPath(request()->query())
        ]);

        return $paginate->setCollection($collect);
}
bobbybouwmann's avatar

@Denisbeder You can also add this method to the collection class using a macro, that way you don't have to use the trait ;) Just my 2 cents :D

anthony107's avatar
	$currentPage = LengthAwarePaginator::resolveCurrentPage();
    $dataCollection = collect($data);
    $perPage = 10;
    $currentPagedata = $dataCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();
    $pdata= new LengthAwarePaginator($currentPagedata , count($dataCollection), $perPage);
    $pdata->setPath($request->url());

Can you help me? I cant open page 2 link

1 like
Previous

Please or to participate in this conversation.