liorocks/pingcrm-react

Pagination breaks when Model Caching is enabled (GeneaLabs/laravel-model-caching)

jjsavrx opened this issue · 3 comments

Hello,

I am using pingcrm-react to test numerous features for an upcoming project that I wish to build within the framework. I am experiencing a unique issue with pagination throwing an error, specifically when I pair a model with the Caching package "GeneaLabs/laravel-model-caching". I have used the caching package outside of the Inertia framework and it works great, however within the framework it has issues. Models by themselves still work great within Inertia with the Caching package enabled (via "use Cachable" declared in the Model's PHP file), however when a model is invoked via Laravel's Eloquent ORM with pagination, I receive the following error: "Serialization of 'class@anonymous' is not allowed", which from what I could gather is tied directly to the framework's code in the file "app/Providers/AppServiceProvider.php" for the "protected function registerLengthAwarePaginator()". I believe because the function uses the line "return new class (...array_values($values)) extends LengthAwarePaginator", it creates an anonymous class which causes the caching to break.

The method in which I am using to test this is adding both "use GeneaLabs\LaravelModelCaching\Traits\Cachable;" (file header) and "use Cachable" (inside the model) to the "Contact" model (in the file "app/Models/Contact.php"). When I access the page "/contacts" I receive the error.

I have also been able to successfully create a LengthAwarePaginator via cached data (directly via Illuminate\Pagination\LengthAwarePaginator), however due to it not being created by the function declared in "AppServiceProvider.php", the front-end does not correctly load the data.

Again this is a unique issue tied to a completely separate Laravel package (GeneaLabs/laravel-model-caching), but ultimately it would be wonderful if they could work in unison.

Please advise. Thank you!

@jjsavrx

Well, when I originally ported this from inertiajs/pingcrm, I tried to keep changes to absolute minimum.

However, after several projects working with inertia, I never used this custom pagination class and I'm thinking to remove it from here as well.

I think there are only few reasons why Jonathan (creator of inertia) did this.

  1. it automatically appends query params to pagination links. such as ?search=name, etc. AppServiceProvider.php#L109
  2. it adds transform and only methods to paginated results. AppServiceProvider.php#L92

So, I think it would be better, if this custom pagination class is removed and the eloquent model transformation should be moved to Laravel's API Resource classes.

That will simplify the process of getting started with inertia/this repo.
Appending query params in pagination links will be as easy as this:

// ...
paginate()->appends(request()->query())

All transformations such as this ContactsController.php#L23 can be moved to API Resources

And users won't face the caching problems such as this one.

See: #24

Thank you for looking into this and updating the code! I really appreciate it.