Pagination with Grandchildren

Hey guys I’ve pretty much followed the tutorials only to make it easier for my clients, I’ve used sub pages as a way of dividing categories, so I then use grandChildren to list them all on the same page, can I paginate normally like this, and if so where have I gone wrong in my code? Thanks - Josh

<section id="vehicles">
    <div class="container-fluid">
        <div class="row p-0 p-lg-5">
            <?php foreach ($page
                    ->grandChildren()
                    ->listed()
                    ->paginate(1)
                    ->sortBy('price', "desc")
                as $vehicles) :
            ?>
                <div class="col-12 col-lg-6 col-xl-3 p-4 p-lg-3 vehicle-card">
                    <a href="<?= $vehicles->url() ?>">
                        <div class="card">
                            <img class="card-img-top" src="<?= $vehicles->images()->findBy("template", "cover")->crop(520, 311)->url() ?>" alt="Card image cap">
                            <div class="card-body my-2 mx-3">
                                <h3 class="card-title"><?= $vehicles->title() ?></h3>
                                <span class="card-text"><?= $vehicles->spec() ?></span>
                                <br />
                                <span class="info"><?= $vehicles->fueltype() ?></span>
                                <span class="info"><?= $vehicles->mileage() ?></span>
                                <span class="info mb-4"><?= $vehicles->gearbox() ?></span>
                                <br />
                                <span class="price">
                                    £<?= number_format($vehicles->price()->toFloat(), 0, '.', ',') ?>
                                </span>
                            </div>
                        </div>
                    </a>
                </div>
            <?php endforeach ?>
        </div>
    </div>
</section>
<nav>
    <a href="<?= $vehicles->pagination()->prevPageUrl() ?>" aria-label="Go to previous page">&larr;</a>
    <a href="<?= $vehicles->pagination()->nextPageUrl() ?>" aria-label="Go to next page">&rarr;</a>
</nav>

/page:2 also is not being generated, or at least says “error no template assigned”

<?php 

$vehicles = $page
                    ->grandChildren()
                    ->listed()
                    ->paginate(1)
                    ->sortBy('price', "desc");

foreach( $vehicles as $vehicle) : ?>

Then inside the loop, change $vehicles to $vehicle, but keep $vehicles for the pagination.

2 Likes

Brilliant thank you so much.

I cannot lie, I couldn’t have chosen a better CMS to make my first ever dynamic website. This community and the admins are so helpful.

1 Like