deepu07's avatar
Level 11

Laravel Pagination render()

Hi Guys, In my project, I'm getting this error

ErrorException (E_ERROR)
Call to undefined method stdClass::render() (View:

Where I'm using custom pagination where After combining two objects in my code applying pagination. here is my pagination code

public function paginateWithoutKey($items, $perPage = 15, $page = null, $options = [])
    {
        $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);

        $items = $items instanceof Collection ? $items : Collection::make($items);

        $lap = new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);

        $res = [
            'current_page' => $lap->currentPage(),
            'data' => $lap->values(),
            'first_page_url' => $lap->url(1),
            'from' => $lap->firstItem(),
            'last_page' => $lap->lastPage(),
            'last_page_url' => $lap->url($lap->lastPage()),
            'next_page_url' => $lap->nextPageUrl(),
            'per_page' => $lap->perPage(),
            'prev_page_url' => $lap->previousPageUrl(),
            'to' => $lap->lastItem(),
            'total' => $lap->total(),
        ];

        return json_decode(json_encode($res));
    }

I'm looping data in blade file and everything working except page links {{ $data->render() }}. can anyone help me on this i.e. how to resolve this issue. Thanks in advance.

0 likes
8 replies
goldtaste's avatar

Your are returning an array that has been converted to the default php stdObject through the json encoding/decoding. stdObject doesn't have a render method.

goldtaste's avatar

Yeah, the default php object doesn't have a render method. Rather than returning an array/stdObject of pagination properties that you have built up, you may be better returning a paginator.

There's more info here: https://laravel.com/docs/5.8/pagination#introduction

You may be re-inventing the wheel as is with the $res array. Can't you just return the paginator?

jlrdw's avatar

Don't use that next previous part take out of the parenthesis.

That example he wanted just next and previous.

deepu07's avatar
Level 11

@jlrdw I jus added like this it is working perfectly href="{{ url()->current().$paginator->nextPageUrl() }}" Thanks!

Please or to participate in this conversation.