In this episode, we're making our board columns draggable so you can reorder them however you like, and that new order actually gets saved back to the database! To do this, we start by using the "sortable JS" library, which is a super popular way to let users drag and drop items on the page (Trello-style!). Since we're using Livewire for the frontend, we also bring in the handy Livewire Sortable JS package for a tighter integration.
We walk through installing the package, bringing it into our JavaScript bundle, and wiring everything up inside our Board Show component. You'll see how we make each column draggable, set up the necessary Livewire directives, and handle the drag events. There are some little hiccups (like hydration issues and fixing keys), but we iron those out together.
Once that's done, we tap into the magic of the Eloquent Sortable package to reorder columns in the database. We collect the new order of column IDs after a drag, then pass them to the model to persist the sort – it's crazy simple! But, we take it a step further and make sure the sorting action is scoped to the currently authenticated user, so people can't mess with each other's boards. After all that, your columns are now fully sortable and up-to-date in the database, just like you'd expect!