Matt McManis
Matt McManis

Reputation: 4675

Pagination Wont Display Next Page Results

I'm using OctoberCMS based on Laravel and Twig.

October has a Scope Pagination feature, but it lacks the functionality I need, so I have to use Laravel to return the results and pagination.

I have a galley with categories and image database records.

I'm trying to get the :category Identifier nature from the URL to filter back database results.


Problem

The code returns the nature images and displays pagination. But when I click a page number the url changes to ?page=2 or /2, but the page button stays on 1 and the records/images don't change.

Its like the php resets on the next page and shows page 1 results again.

enter image description here


OctoberCMS

URL
localhost/gallery/nature

Identifiers
/gallery/:category?latest/:page?


Laravel

Get all image records in category nature.

I put this code in my Page gallery.htm.

$category = $this->param('category');
$this['scopeCategory'] = Gallery::where('category', $category)->paginate(5);

Twig

Display the images with pagination.

List Images

{% for image in scopeCategory %}
    <img src="example.com/{{ image.name }}.jpg">
{% endfor %}

Simple Pagination

<< 1 | 2 | 3 | 4 >>

{{ scopeCategory.render|raw }}

Custom Pagination

← Prev 2 | 3 | 4 | 5 Next →

{% if scopeCategory.LastPage > 1 %}
    <ul class="pagination">
        {% if scopeCategory.CurrentPage > 1 %}
            <li><a href="{{ this.page.baseFileName|page({ (pageParam): (scopeCategory.CurrentPage-1) }) }}">← Prev</a></li>
        {% endif %}
        {% for page in 1..scopeCategory.LastPage %}
            <li class="{{ scopeCategory.CurrentPage == page ? 'active' : null }}">
                <a href="{{ this.page.baseFileName|page({ (pageParam): page }) }}">{{ page }}</a>
            </li>
        {% endfor %}
        {% if scopeCategory.LastPage > scopeCategory.CurrentPage %}
            <li><a href="{{ this.page.baseFileName|page({ (pageParam): (scopeCategory.CurrentPage+1) }) }}">Next →</a></li>
        {% endif %}
    </ul>
{% endif %}

Upvotes: 2

Views: 2801

Answers (1)

Nick
Nick

Reputation: 2641

You are able to pass the page number directly to the paginator by specifying it as a second parameter (see docs). The following code should fix your issue.

$page= $this->param('page');
$category = $this->param('category');
$this['scopeCategory'] = Gallery::where('category', $category)->paginate(5, $page);

Upvotes: 1

Related Questions