Skip to content

Instantly share code, notes, and snippets.

@joydeep-bhowmik
Created July 2, 2024 09:04
Show Gist options
  • Save joydeep-bhowmik/5771b425b078270b5f5d56c01e2c5d88 to your computer and use it in GitHub Desktop.
Save joydeep-bhowmik/5771b425b078270b5f5d56c01e2c5d88 to your computer and use it in GitHub Desktop.
livewire sortable
$updateOrder = function ($items) {
    $ids = collect($items)->pluck('value')->toArray();

    Photo::whereIn('id', $ids)
        ->get()
        ->each(function ($item) use ($ids) {
            $item->update(['ordering' => array_search($item->id, $ids)]);
        });

    $this->photos = collect($ids)
        ->map(function ($id) {
            return $this->photos->firstWhere('id', $id);
        })
        ->filter();
    $this->photos = $this->photos->values();
};
<ul wire:sortable="updateTaskOrder">
    @foreach ($tasks as $task)
        <li wire:sortable.item="{{ $task->id }}" wire:key="task-{{ $task->id }}">
            <h4 wire:sortable.handle>{{ $task->title }}</h4>
            <button wire:click="removeTask({{ $task->id }})">Remove</button>
        </li>
    @endforeach
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment