Maaz Zindani

Loading the index…

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

Laravel BadMethodCallException: Method paginate does not exist

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

Short answer

paginate() is a method of the query builder and of Eloquent queries. If you call get() or all() first, you hold a Collection, which has no paginate method. Move paginate() to the end of the query in place of get(). For data already in memory, build a LengthAwarePaginator by hand.

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

Fix it fast

  1. 1Find out which cause you have

    Find the line that calls paginate() and look at what is on its left. If a get(), all() or toArray() comes before it, the query already ran.

    grep -rn "paginate(" app/ routes/
  2. 2Match what you see, then jump to its fix

    1. 1The exception names Illuminate\Support\Collection or Illuminate\Database\Eloquent\Collection.get() or all() ran before paginate()Fix
    2. 2The rows come from an API call, a merge of two queries, or a filter() on a collection.The data is a plain array or a collection built in PHPFix

What does the error look like?

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

Cause 1 of 2

get() or all() ran before paginate()

1Check

Print the type on the left of the call. A paginate-able object is a query builder; a Collection is not.

// in tinker
>>> get_class(App\Models\User::where('active', 1));
>>> get_class(App\Models\User::where('active', 1)->get());

2Fix

Delete get() or all() and end the chain with paginate(). It sets the limit and offset from the page query parameter.

// before
$users = User::where('votes', '>', 100)->get()->paginate(15);

// after
$users = User::where('votes', '>', 100)->paginate(15);

3Verify

Load the page and open ?page=2.

curl -s "http://localhost:8000/users?page=2" | head

Confirmed by laravel.com

Did this fix it?

Cause 2 of 2

The data is a plain array or a collection built in PHP

1Check

If the rows cannot come straight from one query, there is nothing for paginate() to put LIMIT on.

grep -rnE "collect\(|->merge\(|->filter\(" app/Http/Controllers/

2Fix

Slice the items yourself and hand them to a LengthAwarePaginator along with the total count. The docs advise slicing the items yourself, for example with array_slice. The class wraps arrays in a Collection.

use Illuminate\Pagination\LengthAwarePaginator;

$perPage = 15;
$page = request()->integer('page', 1);
$items = $all->slice(($page - 1) * $perPage, $perPage)->values();

$paginator = new LengthAwarePaginator(
    $items, $all->count(), $perPage, $page,
    ['path' => request()->url(), 'query' => request()->query()]
);

3Verify

Render links() in the view and check the page links.

php artisan route:list --path=users

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Treat get() as the end of a query: sort, filter and paginate before it.
  • Use simplePaginate() when you only need Next and Previous, so Laravel skips the count query.
  • Call withQueryString() on the paginator so filters survive between pages.

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.