sreeharshrajan's avatar

Multiple Pagination Instance Support Fix for Custom Pagination Method

Hello Laravel Developers,

I have been using the following Pagination Helper to modify the built-in pagination method, but unfortunately this doesn't seem to work with Multiple Pagination Instance in a Single Page (2 paginations in 1 page).

public static function paginate($items, $perPage = 15, $path = '')
    {
        $options = [
            'path' => $path
        ];
        $page = Paginator::resolveCurrentPage();
        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    }

Looking forward to someone solve the issue.

0 likes
7 replies
Sinnbeck's avatar

Please format your code by adding ``` on the line before and after it.

How are you using this paginator?

1 like
sreeharshrajan's avatar

@Sinnbeck I have used the above paginator after the paginate query and mapping to another variable to add along the return response.

 $var = $var->paginate(15, ['*'], 'page');

 if ($withBalance) {
            $var = $var->map(function($item) {
                $item->balance = $this->getBalance($item->uuid);
                return $item;
            });
            $var = PaginationHelper::paginate($var, 15, 'path/');
 }
Sinnbeck's avatar

@sreeharshrajan Hard to guess what $var is here? If its an eloquent query, why write your own paginator? If you need to format the code, use either an accessor or an api resource

1 like
sreeharshrajan's avatar

@Sinnbeck yes, it's an eloquent query. I had to use a custom paginator since I have modified the query with an extra variable and it was not showing up in the blade with paginations

Sinnbeck's avatar

@sreeharshrajan then use an accessor to get the data or similar. Or you can even use ->through() after the paginate() call

1 like
Sinnbeck's avatar

If you want to use that code with multiple paginators on page, you need to pass a unique name for each

public static function paginate($items, $perPage = 15, $path = '', $name = 'page') //add $name
    {
        $options = [
            'path' => $path
        ];
        $page = Paginator::resolveCurrentPage($name); //pass it here
        return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
    }
1 like

Please or to participate in this conversation.