You need to exclude (and order) them in the query
$users = User::whereNotIn('id', [1])->orderBy('id')->paginate(15);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
You need to exclude (and order) them in the query
$users = User::whereNotIn('id', [1])->orderBy('id')->paginate(15);
Please or to participate in this conversation.