Lookitsatravis\Listify\Listify::shufflePositionsOnIntermediateItems PHP Method

shufflePositionsOnIntermediateItems() private method

Reorders intermediate items to support moving an item from old_position to new_position.
private shufflePositionsOnIntermediateItems ( integer $old_position, integer $new_position, string $avoid_id = NULL ) : void
$old_position integer
$new_position integer
$avoid_id string You can pass in an ID of a record matching the current class and it will be ignored
return void
    private function shufflePositionsOnIntermediateItems($old_position, $new_position, $avoid_id = NULL)
    {
        if ($old_position == $new_position) {
            return;
        }
        $avoid_id_condition = $avoid_id ? $this->primaryKey() . " != " . $avoid_id : '1 = 1';
        if ($old_position < $new_position) {
            // Decrement position of intermediate items
            // e.g., if moving an item from 2 to 5,
            // move [3, 4, 5] to [2, 3, 4]
            $this->listifyList()->where($this->positionColumn(), '>', $old_position)->where($this->positionColumn(), '<=', $new_position)->whereRaw($avoid_id_condition)->decrement($this->positionColumn());
        } else {
            // Increment position of intermediate items
            // e.g., if moving an item from 5 to 2,
            // move [2, 3, 4] to [3, 4, 5]
            $this->listifyList()->where($this->positionColumn(), '>=', $new_position)->where($this->positionColumn(), '<', $old_position)->whereRaw($avoid_id_condition)->increment($this->positionColumn());
        }
    }