aarondfrancis / fast-paginate

A fast implementation of offset/limit pagination for Laravel.
MIT License
1.21k stars 56 forks source link

Method Illuminate\Database\Eloquent\Collection::fastPaginate does not exist. #23

Closed renege closed 2 years ago

renege commented 2 years ago

Hi @aarondfrancis,

Promising package!

But I installed it on my Laravel 9 project (composer require hammerstone/fast-paginate).

And tries to run all 3 options:

  public function index()
    {
        return User::query()->get()->fastPaginate();
        return new UserCollection( User::query()->get()->fastPaginate());
        return new UserCollection( User::all()->fastPaginate());
    }

And keep getting:

Method Illuminate\Database\Eloquent\Collection::fastPaginate does not exist.

aarondfrancis commented 2 years ago
return User::query()->get()->fastPaginate();
return new UserCollection( User::query()->get()->fastPaginate());
return new UserCollection( User::all()->fastPaginate());

You're paginating after the query has already run. You should paginate the query itself!

return User::query()->fastPaginate();
garethnic commented 2 years ago

I would add that on fresh Laravel 8/9 projects, it works but on an existing Laravel 8 project of mine it throws a similar issue. I'm assuming there's probably a package(s) messing with it. Anyone running into this should double check existing packages or possible overrides being set somewhere.

aarondfrancis commented 2 years ago

@garethnic wait it works on the collection? That's... interesting. I might have to look into this then!

garethnic commented 2 years ago

My bad on the clarity part. return User::query()->fastPaginate(); works on fresh installations of Laravel 8/9. Installing in one of my old Laravel (8.83.23) projects is where the issue comes in. Though I experienced it complaining about not finding the service provider. The project being a few years old, it has a bunch of packages and overrides going on so it would be a deep to figure out the root cause.

aarondfrancis commented 2 years ago

I tried to implement a fastPaginate on the Collection class, but unfortunately there's no paginate to model fastPaginate after. There is a forPage but that's kinda different. So I don't think there's much I can do here. Thanks for opening the issue though! Lemme know if you need anything else.