Ayam Geprek
Ayam Geprek

Reputation: 151

How to paginate laravel

Hei I got some problem with pagination so I use to be make pagination with this simple code

$posts = Auth::user()->posts()->latest()->paginate(5);

and it will paginate automatically

but when I use code like this

$count = $this->getCountPost()->paginate(5);

it will retrieve error

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

this is my getcountpost function

private function getCountPost(){
    $user = Auth::user();
    $posts = $user->posts;
    foreach ($posts as $key => $value) {
        $posts[$key]->comment_to_post = Post::where('id_post', $value->id)->count();
    }
    return $posts;

hope you can help me guys

Upvotes: 2

Views: 723

Answers (3)

Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

For this there are several approaches avalable.

Method 1:

For paginating a collection, Laravel provides a method called forPage

https://laravel.com/api/master/Illuminate/Support/Collection.html#method_forPage

Try the following method and may be this is what you're looking for.

return $this->getCountPost()->forPage(1, 10);

For this scenario, you'll have to buld your own logic to add the links to paginate.

however, the following approach will make life more easier.

Method 2:

Using Laravel's LengthAwarePaginator

namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
 
use App\Http\Requests;
 
class ItemsController extends Controller
{
     public function items(Request $request)
    {
        $items = [
            'item1',
            'item2',
            'item3',
            'item4',
            'item5',
            'item6',
            'item7',
            'item8',
            'item9',
            'item10'
            ];
 
        // Get current page form url e.x. &page=1
        $currentPage = LengthAwarePaginator::resolveCurrentPage();
 
        // Create a new Laravel collection from the array data
        $itemCollection = collect($items);
 
        // Define how many items we want to be visible in each page
        $perPage = 1;
 
        // Slice the collection to get the items to display in current page
        $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();
 
        // Create our paginator and pass it to the view
        $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage);
 
        // set url path for generted links
        $paginatedItems->setPath($request->url());
 
        return view('items_view', ['items' => $paginatedItems]);
    }

And on the blade file

 <h1>Items List</h1>
 
<ul>
@foreach ($items as $item) 
   <li> {{ $item }} </li>
@endforeach
</ul>
 
<div>
{{ $items->links() }}
</div>

Reference: https://arjunphp.com/laravel-5-pagination-array/

Upvotes: 1

Shailendra Gupta
Shailendra Gupta

Reputation: 1118

the $post variable contains the total no of page count and you can use like this $post->total

Upvotes: 0

Wreigh
Wreigh

Reputation: 3287

You can add a helper function on your Post model to count what you wanted to count on each post.

public function getCommentsToPostCount($id)
{
    return Post::where('id_post', $id)->count();
} // this is an alternative for your `for` loop.

And then, let's update your getCountPost

private function getCountPost(){
    return Auth::user()->posts();
}

Now on your usage, you can now:

$pages = $this->getCountPost()->paginate(5);

Upvotes: 0

Related Questions