Numbering and Reordering Photos in Laravel

Resumo

Pixel shares a DEV Community post about adding numbered, reorderable photos to a Laravel boat-building log. It covers matching image badges to lightbox counts, handling single-photo entries, and solving the UI challenge that emerged after an import merged captions.

DEV Community

Numbering photos is easy. Letting people reorder them is not

I run Boat Build Log, a free site where people building a boat at home keep a dated log of the work: a few paragraphs, a handful of photos, repeat for two years. It's a Laravel app with server-rendered Blade and small plain JavaScript files, no framework on the front end. This post is about a feature that sounds like an afternoon's work: let a builder put the photos on an entry in order, and number them. It took three commits, one embarrassing CSS bug, and it only exists because an import went wrong. Why the photos needed numbers A builder moved his log over from his own website. On his old site every page was a column of photos with a caption under each one. After the import, each entry was one block of text followed by a grid of pictures. All the captions had run together and nothing said which note went with which photo. He wrote to me, quite reasonably, and asked how a reader was supposed to know that "fitting the sheer clamps" was the fourth picture and not the fifth. The cheap fix was a number. If every photo shows its position on its corner, the text can say 3. and mean one picture. The lightbox already counted photos ("3 of 9"), so the number on the page just had to match it. Step 1: number what's already there This part really was small. In the entry partial: {{-- Numbered so the writing can point at a photo. The number is the
photo's position, the same one the lightbox counts. --}}
@if($photos->count() > 1)
{{ $loop->iteration }}
@endif
A single photo gets no badge, since "photo 1" of one is just noise. The badge sits over the corner of the picture and not underneath it, so the grid doesn't grow a row: .photo { position: relative; }
.photo-num {
position: absolute; top: 0.35rem; left: 0.35rem; z-index: 1;
border-radius: 999px; background: rgba(12, 13, 15, 0.72); color: #fff;
pointer-events: none; / a tap still opens the lightbox /
}
The site can also turn a log into a printed book, and the book had to use the same numbers or "see 3" would point at nothing on paper. There the photos get "Photo 1", "Photo 2" in the caption line. One numbering, three places: the page, the lightbox and the book. I shipped that and felt pretty good for about four days. The number was useless without a way to choose it The position came from a sort_order column on the media table, and sort_order was set by whatever order the phone handed the files over. Usually that's the order they were taken. Sometimes it's the reverse. Sometimes it's the order you tapped them in the picker. So a builder would write "1. dry fit, 2. glued up, 3. clamps off" and the page would show the clamps coming off first. A number is only worth having if the person writing the entry decides which photo gets it. Step 2: one gesture, three grids Photos show up in three places while you're writing: Where What's in the grid What "order" means New entry composer File objects not sent yet The order they'll be uploaded Edit form, "add photos" File objects not sent yet Same Edit form, saved photos Rows with ids sort_order in the database I didn't want three implementations, so there's one small script, photo-order.js, that knows nothing about files or ids. You hand it a grid of elements and a callback. It adds a number and two arrow buttons to each figure, makes them draggable, and after every move it tells you the new order as a list of figures. What you do with that is your business. The composer keeps a pending array of files, so its callback is one line: window.bblPhotoOrder.decorate(preview, function (figs) {
// pending follows the grid, so upload order = numbers on the page
pending = figs.map(function (f) { return f._file; }).filter(Boolean);
});
The saved-photos grid is even lazier. Each figure holds a hidden order[] input with the photo's id. Browsers submit inputs in document order, so once the figures have moved in the DOM, the form already carries the new order. The callback only has to reveal the Sa...
Numbering and Reordering Photos in Laravel
Powered by Readybot.io

As últimas novidades de Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …