spatie/eloquent-sortable

Don't override order on create when provided

FrittenKeeZ opened this issue · 4 comments

If a specific order is provided when creating a model, fx. order_column => 3, then it will be overridden with the highest order by default.

Changing SortableTrait::shouldSortWhenCreating() would accommodate this:

/**
 * Determine if the order column should be set when saving a new model instance.
 *
 * @return bool
 */
public function shouldSortWhenCreating(): bool
{
    if ($this->isDirty($this->determineOrderColumnName())) {
        return false;
    }

    return isset($this->sortable['sort_when_creating'])
        ? $this->sortable['sort_when_creating']
        : config('eloquent-sortable.sort_when_creating', true);
}
suth commented

Ran into this issue today while writing a test, but I ended up working around it by just ignoring the ordering and using setNewOrder() after creating all the models. This actually ended up feeling cleaner in my case, but I can still see a use case for being able to explicitly provide the order when creating a model in one-off situations.

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

I having a similar case where we want to duplicate records and expectedly the order must be the same. Unfortunately this appears as an issue. Although it is easy to override the method and work this around I think that the proposed solution from @FrittenKeeZ is a good one. Or at least there should be a way to dynamically set this on the model.

@freekmurze any thoughts on this?