Laravel BadMethodCallException: Method paginate does not exist
BadMethodCallException: 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.
Fix it fast
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/2Match what you see, then jump to its fix
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" | headConfirmed by laravel.com
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=usersConfirmed by laravel.com
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 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 fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix