kickthemooon's avatar

Redis and pagination

I am starting off with redis. This is my controller:

public function index(){

        $currentPage = Input::get('page') ? Input::get('page') : '1';

        $articles = $this->remember('articles.paginate.'.$currentPage, 3, function(){
            return json_encode(Article::orderBy('id', 'desc')->with('categories')->paginate(20));
        });

        return view('index')->with(compact('articles'));
    }

    public function remember($key, $time, $callback){
        if($value = LRedis::get($key)){
            return json_decode($value);
        }

        LRedis::setex($key, $time, $value = $callback());

        return json_decode($value);
    }

and this is my view:

@foreach($articles->data as $article)
            <div class="row">
                <div class="col-sm-12">
                    <a href="/article/{{ $article->id }}"><h3 class="title">{{ $article->title }}</h3></a>
                    <p class="text-muted">
                        @foreach($article->categories as $category) <a href="/category/{{ $category->id }}">{{ $category->name }}</a> @endforeach
                    </p>
                    <p>
                        {{ $article->content }}
                    </p>
                </div>
            </div>
            <hr>
        @endforeach

        <div class="pull-right">
            {{ $articles->render() }}
        </div>

The render method for pagination is not working and I am getting this error:

Call to undefined method stdClass::render() 

This is my $articles die and dump:

{#198 ▼
  +"total": 11683
  +"per_page": 20
  +"current_page": 1
  +"last_page": 585
  +"next_page_url": "http://redis.dev?page=2"
  +"prev_page_url": null
  +"from": 1
  +"to": 20
  +"data": array:20 [▶]
}

I would be interested in some suggestions or solutions for the pagination part

0 likes
3 replies
willvincent's avatar

should be $articles->links() not $articles->render()

kickthemooon's avatar

@willvincent $articles->render() is working in an other case for me except with redis as shown above.

$articles->links() throwing the same error.

Call to undefined method stdClass::links()

kickthemooon's avatar
kickthemooon
OP
Best Answer
Level 5

I was trying to get redis and laravel going with phpredis and the vetruvet/laravel-phpredis package.

Things didn't really want to go as expected.

Cache::remember and other Cache methods were giving me false.

Switched over to tillkruss/laravel-phpredis

PHPRedis and Laravel work great now.

Please or to participate in this conversation.