eloquent-sortable icon indicating copy to clipboard operation
eloquent-sortable copied to clipboard

moveToStart updates wrong rows

Open quelo83 opened this issue 4 months ago • 0 comments

Hi, the method "moveToStart" doesn't seem to work well. It increments all rows except the current model and this is not correct. The current code is:

$this->$orderColumnName = $firstModel->$orderColumnName;
$this->save();

$this->buildSortQuery()->where($this->getKeyName(), '!=', $this->getKey())->increment($orderColumnName);

If I'm not wrong, the query should have another where clause to scope and increment only the previous rows, something like this:

$previousOrder = $this->$orderColumnName;

$this->$orderColumnName = $firstModel->$orderColumnName;
$this->save();

$this->buildSortQuery()
    ->where($this->getKeyName(), '!=', $this->getKey())
    ->where($this->getKeyName(), '<', $previousOrder)
    ->increment($orderColumnName);

Please, let me know if I'm missing something.

Thanks in advance!

quelo83 avatar Feb 08 '24 16:02 quelo83