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

msaied's avatar
Level 10

How to paginate Text ?

Hello i have long text like

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
---
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
---
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
---
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

I'd like to split this text by --- so I got an array with 4 rows

array("It is a long...", "It is a long...", "It is a long...", "It is a long..")

I'd like to paginate those text

0 likes
8 replies
msaied's avatar
Level 10

I know how to convert them to an array but my question is how to convert array to laravel pagination Like

->paginate()
SilenceBringer's avatar
Level 55

@msaied

new \Illuminate\Pagination\LengthAwarePaginator($array, $total, $perPage)

you can do something like

$total = count($data);
$perPage = 5;
$page = 1;

$pagination = new \Illuminate\Pagination\LengthAwarePaginator(collect($array)->skip(($page - 1) * $perPage)->take($perPage), $total, $perPage)
msaied's avatar
Level 10

thanks a lot , it's work ♥

msaied's avatar
Level 10

i'd like to use laravel ->paginate()

MichalOravec's avatar

@msaied You can create an macro for collection

Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
    $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

    return new LengthAwarePaginator($this->forPage($page, $perPage), $total ?: $this->count(), $perPage, $page, [
        'path' => LengthAwarePaginator::resolveCurrentPath(),
        'pageName' => $pageName,
    ]);
});

You need to add that macro to the Service provider inside boot method.

Then you will be able to paginate a collection

$data = collect($array)->paginate(5);
1 like
msaied's avatar
Level 10

thanks i appreciate your reply

Please or to participate in this conversation.