Support the ongoing development of Laravel.io →
Cache Eloquent

I am using paginater with cache when building the query like this:

$comments = Comment::where('course_id', '=', $course->id)
					->remember(5, 'key')
					->orderBy('created_at', 'DESC')
					->paginate(10);

However, it doesn't work as expected because the attributes are missing. I var_dump the object for a record retrieved:

object(Comment)[258]
  protected 'softDelete' => boolean true
  protected 'guarded' => 
    array
      empty
  protected 'connection' => null
  protected 'table' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array
      'aggregate' => int 4
...

If I don't supply the key for the cache, it will return something like this (for a record) and it works:

object(Comment)[258]
  protected 'softDelete' => boolean true
  protected 'guarded' => 
    array
      empty
  protected 'connection' => null
  protected 'table' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array
      'id' => int 461
      'course_id' => int 8
      'semester' => string '1213B' (length=5)
      'instructor' => string '.....' (length=22)
      'grade' => string 'C+' (length=2)
      'workload' => int 3
      'body' => string '<p>.......</p>' (length=95)
      'admin_note' => null
      'ip_address' => string '.......' (length=14)
      'password' => null
      'created_at' => string '2013-06-10 00:55:57' (length=19)
      'updated_at' => string '2013-06-10 00:55:57' (length=19)
      'deleted_at' => null
...

How to set a key for caching paginated results so that I can remove it when the data is updated later?

Last updated 2 years ago.
0

+1

I'm having the same problem.

This works:

User::rememberForever()->paginate(Input::get('per_page', 20));

This doesn't:

User::rememberForever('key')->paginate(Input::get('per_page', 20));

The data I'm getting back from this query is "[{"aggregate":"2"}]".

Where 2 is the number of User in my database.

Laravel 4.1.30.

Last updated 2 years ago.
0

+1 Seems to be an issue in the paginator with using Cache keys instead. Which is an interesting problem.

I would quite like to know if anyone has found a solution for this, as a web app I'm implementing that makes use of pagination needs to be able to clear the Pagination cache for a number of models

0

Sign in to participate in this thread!

Eventy

Your banner here too?

ericksli ericksli Joined 17 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.