Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Building a Drag and Drop List with Livewire v4

Reorder items with drag and drop.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Component

Build a sortable list with SortableJS.

Component

php
@php
new class extends Livewire\Component {
    public array $items = [];
    
    public function updateOrder(array $order): void
    {
        foreach ($order as $position => $id) {
            Item::find($id)->update(['position' => $position]);
        }
    }
}
@endphp

<div 
    x-data
    x-init="Sortable.create($el, { onEnd: (e) => $wire.updateOrder([...e.target.children].map(c => c.dataset.id)) })">
    @foreach($items as $item)
        <div data-id="{{ $item->id }}">{{ $item->name }}</div>
    @endforeach
</div>
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read