Keeping Kanban Card Positions Consistent in Laravel

Moving a card between columns looks trivial until two columns need renumbering at once and something fails halfway. This is how I keep positions contiguous in Laravel and prove it with a test.

7 min readBy Saad Ali KhanOct 2026
Keeping Kanban Card Positions Consistent in Laravel

The problem

A Kanban board stores each task with a status (its column) and a position (its place in the column). Dragging a card to a new column and index means: put it in the new column at that index, shift the cards after it down by one, and close the gap it left behind in the old column. That is several writes, and they must succeed or fail together.

If they do not, you get duplicate positions, gaps, or a card that exists in neither column, and the board looks randomly shuffled after a refresh.

The rule: positions are always 0 to n-1 per column

Instead of storing fractional positions or trying to patch numbers, I keep a simple invariant: inside every project and status column, positions are contiguous, starting at zero. To keep it, rebuild the column's order as a list of ids and renumber from that list.

A small service owns the logic

class TaskOrdering
{
    /** Move a task to a column and index. Index is clamped to the column size. */
    public function move(Task $task, TaskStatus $status, int $position): Task
    {
        return DB::transaction(function () use ($task, $status, $position) {
            $oldStatus = $task->status;

            $ids = Task::where('project_id', $task->project_id)
                ->where('status', $status->value)
                ->where('id', '!=', $task->id)
                ->orderBy('position')->orderBy('id')
                ->pluck('id')->all();

            $position = max(0, min($position, count($ids)));
            array_splice($ids, $position, 0, [$task->id]);

            $task->status = $status;
            $task->save();
            $this->renumber($ids);

            if ($oldStatus !== $status) {
                $old = Task::where('project_id', $task->project_id)
                    ->where('status', $oldStatus->value)
                    ->orderBy('position')->orderBy('id')
                    ->pluck('id')->all();
                $this->renumber($old);
            }

            return $task->refresh();
        });
    }
}

Three details matter:

  • The whole move is one transaction. Either both columns are renumbered or neither is.
  • The index is clamped. A client that sends position 999 puts the card at the end instead of creating a gap.
  • Ties are broken by id. Ordering by position then id keeps the result deterministic even if old data had duplicates.

Validate and authorize before you reorder

The controller is thin. A Form Request validates status against the enum and position as a non-negative integer, and a Policy makes sure the task belongs to the current user. Bad input returns a 422 with field errors, and someone else's task returns 403, before the service runs.

Prove the rollback with a test

The interesting test is the failure case. This one makes the third UPDATE throw, in the middle of renumbering, and asserts that every row is exactly as it was:

public function test_failed_move_rolls_back_in_a_transaction(): void
{
    $todo = $this->column('todo', 3);
    $this->column('doing', 2);

    // Let the first two UPDATEs succeed, then blow up mid-renumbering.
    $updates = 0;
    DB::listen(function ($query) use (&$updates) {
        if (str_starts_with($query->sql, 'update "tasks"') && ++$updates === 3) {
            throw new \RuntimeException('boom');
        }
    });

    $before = Task::orderBy('id')->get(['id', 'status', 'position'])->toArray();

    try {
        app(TaskOrdering::class)->move($todo[0], TaskStatus::Doing, 0);
        $this->fail('expected exception');
    } catch (\RuntimeException $e) {
        $this->assertSame('boom', $e->getMessage());
    }

    $this->assertSame($before, Task::orderBy('id')->get(['id', 'status', 'position'])->toArray());
}

Using DB::listen to inject a failure is a handy trick: it proves the transaction boundary really wraps all the writes, without mocking the database.

Trade-offs

  • It rewrites rows. Renumbering updates every card in the affected columns. That is fine for boards with dozens or hundreds of cards. For very large lists, use fractional or lexicographic ranks (as some tools do) and rebalance occasionally.
  • Concurrent moves. Two users moving cards in the same column at once can interleave. If that matters, lock the project's tasks with lockForUpdate() inside the transaction.

The whole API, 36 feature tests and a React front end are in the open-source TaskFlow case study.

Original article written by Saad Ali Khan. Code samples are illustrative and free to adapt in your own projects.

Want this done right in your project?

Send a short description and I'll reply by email with questions or a suggested approach.