简体   繁体   中英

Get total pages of pagination Laravel and write to file page number

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 links posts of current page with file name: file-posts-$page.txt
 }

How I can do it?

I tried:

for ($currentPage = $posts->perPage(); $currentPage <= $posts->total(); $currentPage++) {
   Paginator::currentPageResolver(function () use ($currentPage) {
            return $currentPage;
   });

   //put on file links of current posts of current page with file name: file-posts-$page.txt
}

But I don't get needed me result. I get 1 file for every 1 post..

If I get you correctly, you want to retrieve the results of all the possible pages. If that's it, you can use a model chunk instead. This is how it will work in your case:

Post::chunk(100, function(Collection $posts, $page) { 
  // Do what you want to do with the first 100 using $posts like this
  foreach($posts as $key => $post) {
   // Do stuff with $post
  }
  // You have access to $page here
  //put on file links of current posts of current page with file name: file-posts-$page.txt
});

Since your per page is 100, I passed 100 to the chunk method which will retrieve the first 100 and then the next 100, on and on like that. The second argument passed to it is a callback that each chunk of 100 results and the current page will be passed to.

You should check out more on the chunk method here

I hope this helps.

You can try this.

$posts = Post::paginate(100);

foreach($posts as $page => $post) {

}
{{$posts->links()}}

as documented here you can use

$paginator->lastPage()  

this is the first item index:

$paginator->firstItem(); // Get the result number of the first item in the results.

this is for last item in current page:

$paginator->lastItem()  //Get the result number of the last item in the results.

for example:

this is controller:

public function index(Request $request)
{
    $perPage = (int)$request->query('per_page') ?: API_DEFAULT_PER_PAGE;
    $posts = Post::orderBy('created_at', 'desc')->paginate($perPage);
    ...
    return view('admin.posts.index', ['posts' => $posts]);
}

this is blade:

@extends('layouts.admin')

@section('title', title('posts'))

@section('content')
<h1 class="fw-200 admin-heading__title">posts</h1>
{{$posts->count()}} of {{ $posts->total() }}
<a class="btn btn-outline-primary" href="{{ route('admin.posts.create') }}">create</a>

<div>
    <table>
         <tbody>
            @forelse($posts as $post)
            ....
            @empty
            <tr>
                <td colspan="6" class="text-muted">No items.</td>
            </tr>
            @endforelse
        </tbody>
    </table>
</div>
{{ $posts->appends(request()->query())->links() }}
@endsection

or use

{{ $posts->links() }}

for index of first item you can use ->first

{{$posts->count()}} of {{ $posts->total() }}

i use this beside pagination or title

Posts: ( from {{$posts->firstItem()}} to {{ $posts->lastItem()}}  in {{ $posts->total() }} item(s))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM