Oops, I mean that I needed to move the last item to be the first. Adjusted your code and got close:
$gallery = $page->gallery()->toStructure();
$last = $gallery->last();
$gallery = $gallery->prepend(-1, $last);
This caused the last item to be duplicated, so I tried the following but it’s giving an error:
foreach ($gallery->not($last) as $item) {
Also question about ->prepend(). If the purpose is to add to the beginning, why do you need to define the key? It would always be at the beginning. If I set to 0, it removes the first item.