Fluber's avatar

Foreach paginate pages Laravel

Hello. I have a code:

 $posts = Post::paginate(100);

How I can foreach pages of pagination and show results? I need in every file write a posts.

 foreach($posts as $page => $post) {
      //put on file current posts of page with file name: file-posts-$page.txt
 }

How I can do it?

0 likes
6 replies
Snapey's avatar

pagination does not sound right here.

If you can reasonably load all posts into memory at once, use chunk() on the collection. If not, then do it with take() and offset() to step through all the posts

jlrdw's avatar

please example

Doesn't make sense, as there are examples in the documentation.

https://laravel.com/docs/5.8/queries

Right on that page is the menu:

quote


    Introduction
    Retrieving Results
        Chunking Results
        Aggregates
    Selects
    Raw Expressions
    Joins
    Unions
    Where Clauses
        Parameter Grouping
        Where Exists Clauses
        JSON Where Clauses
    Ordering, Grouping, Limit, & Offset
    Conditional Clauses
    Inserts
    Updates
        Updating JSON Columns
        Increment & Decrement
    Deletes
    Pessimistic Locking

unquote

So why would someone need to give example, when Taylor has already.

Snapey's avatar

Do you need the post formatting in this text file?

luis_carlos's avatar

To show pagination, put this in your view, where you want display pagination links

{{ $posts->links() }}

I guess it will be better if try explain what are you trying to achieve, so we can help better.

Please or to participate in this conversation.