Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel Blade fails on $items->links() because the variable is a Collection

ErrorBadMethodCallException: 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.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The class is Illuminate\Database\Eloquent\Collection, and the controller line ends with ->get() or ::all()The query ends in get() or all()Fix
    2. 2The code in the view reads $user->posts->links()links() is called on a relationship propertyFix
    3. 3The 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. 4The 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

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/Controllers

2Fix

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

Did this fix it?

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/views

2Fix

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

Did this fix it?

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)" app

2Fix

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

Did this fix it?

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/Controllers

2Fix

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

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.