How to create virtual pages for each photo in an album?

Yes, I guess you have to sort them explicitly, maybe by num, but not sure.

Hmm… has no affect. If i dump the page, the images are not in number order in the children array, so for some reason its not seeing the num somehow. If i go into an indidual image page and dump it, i do get the right number back… frustrating.

I tried sortBy() using num, but it does not work.

Hm, if you don’t find a solution in the meantime, I’ll have a closer look later. Gotta do some grocery shopping now.

Fair enough… have fun :slight_smile:

Hm, sorting by num in the template works for me.

foreach ($page->children()->sortBy('num', 'desc') as $child) {
  dump($child->title() . $child->num());

On a side note, You shouldn’t use a model that is different from the template, though.

Sorting works now! I tried what you said myself earlier, and it i didnt work. I think i fixed it by turning the computer off and going outside…

Oh, yes ive been switching things around. Thanks for spotting.

@texnixe I had to switch the sort to this:

$image->sort()->isNotEmpty() ? $image->sort()->value() : 0

Using null upset the next and previous buttons since it means $page->hasNextListed() comes back false.

You mean in the Panel, I guess?

No, in a template…

<nav class="side-nav-left">
  <?php if ($page->hasPrevListed()): ?>
    <a class="btn" href="<?= $page->prevListed()->url() ?>"><?= svg('assets/images/LeftArrow.svg') ?></a>
  <?php else: ?>
    <a class="btn" href="<?= $page->parent()->url() ?>"><?= svg('assets/images/Grid.svg') ?></a>
  <?php endif ?>

It wasnt giving me the next page button, always the one in the else to take you back up a level to an overview page.

Edit… actually its worse than that, its giving me the next alpha order page, not the next listed… sigh. The nav buttons dont seem to be in sync with the page order now.

I’d either use null and then hasNext()/hasPrev() on a sorted collection, or map a value to all pages that don’t have a sort value in the model, or make sure that each image gets a sort number on upload through a hook (if you only upload through the Panel, not via FTP).

Using a different numbering scheme for the same collection is bound to cause issues in the long run.

I finally managed to sort this out by doing this in the controller:


return function ($page) {

  // Get the album images
  $shotlist = $page->parent()->children()->sortBy('num', 'asc');

  // Get the current shot in the album
  $shot = $page->parent()->images()->findBy('name', $page->slug());

  // Image Index
  $shotidx = $shotlist->indexOf($page);

  // Next & Previous Navs
  $next = $page != $shotlist->last()  ? $shotlist->nth($shotidx + 1) : false;
  $prev = $page != $shotlist->first() ? $shotlist->nth($shotidx - 1) : false;

  return [
    'shot' => $shot,
    'shotlist' => $shotlist,
    'next' => $next,
    'prev' => $prev,
    'shotidx' => $shotidx