vincent15000's avatar

Sorting logic

Hello,

I have a code which seems correct to reorder some steps, but the result is not good.

I receive the right old and new index. The order in the database starts at 0, exactly like the index.

public function sortSteps(Request $request, Quest $quest)
{
    $oldOrder = $request->oldIndex;
    $newOrder = $request->newIndex;

    $step = Step::where('quest_id', $quest->id)->where('order', $oldOrder)->first();

    if ($newOrder > $oldOrder) {
        Step::where('quest_id', $quest->id)->where('order', '<=', $newOrder)->where('order', '>', $oldOrder)->decrement('order');
    } else {
        Step::where('quest_id', $quest->id)->where('order', '>=', $newOrder)->where('order', '<', $oldOrder)->increment('order');
    }

    $step->update(['order' => $newOrder]);
}

Do you see any error ?

Thanks for your help.

V

0 likes
1 reply

Please or to participate in this conversation.