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

Penkowsky's avatar

Pagination error when trying to limit the query results. Method Illuminate\Database\Eloquent\Collection::links does not exist.

Pagination works perfectly when including ALL results of a query, but errors out with the following error (below) when I try to exclude a few results in a query.

BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::links does not exist

Example #1: Here is the controller code that works

class UserController extends Controller
{
   public function index()
   {
	   $users = User::paginate(15);
	}
}

Example #2: Here is controller code that errors out when I try to exclude certain data (in this case, Auth::id())

class UserController extends Controller
{
   public function index()
   {
	   $users = User::paginate(15)->except(Auth::id())->sortBy('id');
	}
}

I even tried this replacement code with the same "links does not exist" error. When I take out the "whereNotIn", the pagination works perfectly.

class UserController extends Controller
{
   public function index()
   {
	   $users = User::paginate(15)->whereNotIn('id', [1])->sortBy('id');
	}
}

Question: Is there any way that I can get pagination to work while excluding certain results in the query?

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to exclude (and order) them in the query

$users = User::whereNotIn('id', [1])->orderBy('id')->paginate(15); 
1 like
Snapey's avatar

When you do this;

$users = User::paginate(15)->whereNotIn('id', [1])->sortBy('id');

You are manipulating the collection after it has been queried from the database. The database query executes at the paginate statement, and everything after that becomes collection methods. You end up with a new collection that does not support pagination.

So, as already advised, make sure paginate is the last statement.

1 like
Penkowsky's avatar

Thanks all! It works now. I will keep this in mind for later

Please or to participate in this conversation.