Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Current page items always show on the first page #48438

Closed
wants to merge 1 commit into from

Conversation

hiidemo
Copy link

@hiidemo hiidemo commented Sep 18, 2023

When paging from the sample data array with Paginator class, $items variable always has data from the first page, without changing according to the current page number.

@driesvints
Copy link
Member

Tests fail here.

@driesvints driesvints marked this pull request as draft September 18, 2023 11:23
@driesvints driesvints changed the title Bugfix: Current page items always show on the first page [10.x] Current page items always show on the first page Sep 18, 2023
@hiidemo
Copy link
Author

hiidemo commented Sep 18, 2023

Tests fail here.

The code below always returns ["one", "two", "three"] regardless of changing $currentPage

use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;

class PaginationSample extends Controller
{
    public function index(Request $request)
    {
        $data = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];

        $currentPage = Paginator::resolveCurrentPage();
        $col = collect($data);
        $perPage = 3;
        $items = new Paginator($col, $perPage, $currentPage);
        $items->setPath($request->url());
        $items->appends($request->all());
        dd($items->items());
        
        // add in view
        // {!! $items->links() !!}
        
        return view('index', compact('items'));
    }
}

@hiidemo hiidemo marked this pull request as ready for review September 19, 2023 05:28
@driesvints
Copy link
Member

I don't feel this change makes sense sorry.

@driesvints driesvints closed this Sep 19, 2023
@dennisprudlo
Copy link
Contributor

@hiidemo i think there is a misunderstanding of how the paginator works. The items you pass as a first parameter into the constructor should be the already paginated items. The $perPage and $currentPage parameters don't tell the class "how to paginate the items" but rather "what state the items represent"

@hiidemo
Copy link
Author

hiidemo commented Sep 19, 2023

@hiidemo i think there is a misunderstanding of how the paginator works. The items you pass as a first parameter into the constructor should be the already paginated items. The $perPage and $currentPage parameters don't tell the class "how to paginate the items" but rather "what state the items represent"

I tried passing the first parameter as paginated items (code example below). The resulting data displays the current page correctly but does not show the button to move to the next page in all cases.

use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;

class PaginationSample extends Controller
{
    public function index(Request $request)
    {
        $data = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];

        $currentPage = Paginator::resolveCurrentPage();
        $col = collect($data);
        $perPage = 3;
        $currentPageItems = $col->slice(($currentPage - 1) * $perPage, $perPage)->all();
        $items = new Paginator($currentPageItems, $perPage, $currentPage);
        $items->setPath($request->url());
        $items->appends($request->all());
        dd($items->items());
        
        // add in view
        // {!! $items->links() !!}
        
        return view('index', compact('items'));
    }
}

@dennisprudlo
Copy link
Contributor

This kind of paginator doesn't know how many items the whole dataset has, so it doesn't know whether there is more data (more pages) or not.

You have to either pass more data than the $perPage parameter specifies (for example 11 items when you have 10 items per page) or simply use a paginator that knows how many items the whole dataset has, like the LengthAwarePaginator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants