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

shimana's avatar

Collection::paginate does not exist

I'm using this package to like system,

https://github.com/renoki-co/befriended

And in this way, I show the posts that the user likes.

$liking = $this->user->liking(Post::class)->where('status',5)->get()

I want to display the last posts that have been liked - so I used the function -> reverse()

Illuminate\Database\Eloquent\Collection {#1773 ▼
  #items: array:16 [▼
    15 => App\Models\Post {#1796 ▶}
    14 => App\Models\Post {#1797 ▶}
    13 => App\Models\Post {#1798 ▶}
    12 => App\Models\Post {#1799 ▶}
    11 => App\Models\Post {#1800 ▶}
    10 => App\Models\Post {#1801 ▶}
    9 => App\Models\Post {#1802 ▶}
    8 => App\Models\Post {#1803 ▶}
    7 => App\Models\Post {#1804 ▶}
    6 => App\Models\Post {#1805 ▶}
    5 => App\Models\Post {#1806 ▶}
    4 => App\Models\Post {#1807 ▶}
    3 => App\Models\Post {#1808 ▶}
    2 => App\Models\Post {#1809 ▶}
    1 => App\Models\Post {#1810 ▶}
    0 => App\Models\Post {#1811 ▶}
  ]
  #escapeWhenCastingToString: false
}

But when I use pagination, I get an error.

dd($liking->paginate($this->perPage));

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

0 likes
9 replies
Snapey's avatar

you must call paginate() instead of get(). Add whatever sorting you need to the query before calling paginate

1 like
shimana's avatar

thanks , I changed the code as below,

$liking = $this->user->liking(Post::class)->where('status',5)->orderBy('created_at','desc')->paginate($this->perPage);

But Sorted by the create_at of the posts. I need to sort by Liker date. Then I used reverese()

$liking = $this->user->liking(Post::class)->where('status',5)->reverse()->paginate($this->perPage);

and the error is displayed

Call to undefined method Illuminate\Database\Eloquent\Relations\MorphToMany::reverse()

🤕 How can I use the reverse() feature before pagination?

shimana's avatar
shimana
OP
Best Answer
Level 1

Thank you all, this code solved my problem.

public function paginate($items, $perPage = 15, $page = null, $options = [])
{
	$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

	$items = $items instanceof Collection ? $items : Collection::make($items);

	return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
Source:https://gist.github.com/vluzrmos/3ce756322702331fdf2bf414fea27bcb
Snapey's avatar

@shimana So not really paginating then? Loading all in memory and then paginating the collection. You may as well just show the user the full list.

shimana's avatar

@Snapey no, It is really paginated. i'm using livewire and using pagination with Infinite Scrolling. Now this code works fine for me! The last posts of the user who liked will be displayed.

my Component:


    public function paginate($items, $perPage = 15, $page = null, $options = [])
    {
        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

        $items = $items instanceof Collection ? $items : Collection::make($items);

        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    }
    public function render()
    {
        $liking =  $this->user->liking(Post::class)->where('status',5)->get()->reverse();
        return view('livewire.dashboard.liking',[
            'liking' => $this->paginate($liking,$this->perPage),
        ])->extends('layouts.app');
    }

Snapey's avatar

@shimana NO, you are still doing $liking = $this->user->liking(Post::class)->where('status',5)->get() which gets every record from the database. You then paginate the results.

1 like
jlrdw's avatar

@shimana test it on a million or more records. You need to implement server side pagination.

2 likes

Please or to participate in this conversation.