Laravel Blade fails on $items->links() because the variable is a Collection
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::links does not exist.Short answer
links() belongs to the paginator objects, not to a Collection. The view received a plain Collection, either because the query ended in get() or all(), because a relationship property was used instead of the relationship method, or because a paginator was passed through map() or filter(). Return a paginator from the controller.
Fix it fast
1Find out which cause you have
Print the class of the variable that calls links(). A paginator class means the cause is not on this page. A Collection class means one of the causes below.
// top of the Blade view, or in the controller before returning dump($items::class);2Match what you see, then jump to its fix
- 1
The class is Illuminate\Database\Eloquent\Collection, and the controller line ends with ->get() or ::all()The query ends in get() or all()Fix - 2
The code in the view reads $user->posts->links()links() is called on a relationship propertyFix - 3
The controller calls paginate() and then ->map(...) or ->filter(...), and the variable after that is a CollectionA paginator was transformed with map() or filter()Fix - 4
The rows come from an API call, a merge of two queries or a filter() on an existing collection, so no query exists to paginateThe data is an array or collection built in PHPFix
- 1
What does the error look like?
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::links does not exist.Method Illuminate\Support\Collection::links does not exist.
Cause 1 of 4
The query ends in get() or all()
1Check
get() and all() run the query and return every matching row as a Collection. Only paginate() and its siblings return an object that knows about pages and can print links.
grep -rnE "(->get\(\)|::all\(\))" app/Http/Controllers2Fix
Replace get() with paginate(), and keep it as the last call in the chain. Use simplePaginate() when you only need Next and Previous links.
// before
$users = User::where('votes', '>', 100)->get();
// after
$users = User::where('votes', '>', 100)->paginate(15);3Verify
The view now receives a LengthAwarePaginator, and links() renders.
dump($users::class);Confirmed by laravel.com
Cause 2 of 4
links() is called on a relationship property
1Check
A relationship read as a property, without parentheses, loads the related rows when it is accessed and gives them to you as a Collection. The method form returns the relationship query, so it can still be paginated.
grep -rnE "->[a-z_]+->links\(" resources/views2Fix
Paginate the relationship in the controller with the method form, and hand the result to the view as its own variable.
// controller
$posts = $user->posts()->paginate(10);
// view
{{ $posts->links() }}3Verify
The view loops over $posts and prints the page links.
dump($posts::class);Confirmed by laravel.com
Cause 3 of 4
A paginator was transformed with map() or filter()
1Check
A paginator forwards methods it does not define to the collection of items it holds, and returns what that collection returns. map() returns a new collection, so the paginator is lost and links() has nothing to answer.
grep -rnE "paginate\([^)]*\)->(map|filter|reject)" app2Fix
Use through(), which changes each item in place and returns the paginator itself.
$users = User::paginate(15)->through(function ($user) {
$user->display_name = strtoupper($user->name);
return $user;
});3Verify
The result is still a paginator.
dump($users::class);Confirmed by raw.githubusercontent.com
Cause 4 of 4
The data is an array or collection built in PHP
1Check
paginate() lives on queries. A list that is already in memory has no query behind it, so there is nothing that can add limit and offset.
grep -rn "collect(" app/Http/Controllers2Fix
Slice the array yourself and wrap the slice in a LengthAwarePaginator, passing the total count, the page size and the current page. Set the path so the links point at this page.
use Illuminate\Pagination\LengthAwarePaginator;
$perPage = 15;
$page = (int) request()->query('page', 1);
$paginator = new LengthAwarePaginator(
array_slice($items, ($page - 1) * $perPage, $perPage),
count($items),
$perPage,
$page,
);
$paginator->withPath(request()->url());3Verify
links() renders page numbers, and the slice changes with ?page=2.
curl -s "http://localhost:8000/items?page=2" | grep -c "page="Confirmed by laravel.com
How do I stop it coming back?
- Decide paging in the query, not in the view: end list queries with paginate(), not get().
- Use through() for any per-row change to a paginator, so the object stays a paginator.
- Page related records with the relationship method, $model->relation()->paginate().
- Pass the paginator to the view under its own name, not mixed into a Collection.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Laravel BadMethodCallException: Method paginate does not exist
Method paginate does not exist
paginate() is a method of the query builder and of Eloquent queries.
2 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fix