Skip to content

Commit

Permalink
1.) added: amend to sortables to array to avoid crashing
Browse files Browse the repository at this point in the history
2.) added: bugfix to bootSortableTrait is not set if not used in model as fallback

Signed-off-by: Oskars Germovs <[email protected]>
  • Loading branch information
Faks committed Nov 22, 2024
1 parent 469cca8 commit 59bd4a1
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

trait SortableTrait
{
public array|null $sortables;
public array $sortables = [];

public static function bootSortableTrait(): void
{
static::creating(function (Model $model) {
if ($model instanceof Sortable && $model->shouldSortWhenCreating()) {
if (($model instanceof Sortable || $model instanceof Model) && $model->shouldSortWhenCreating()) {
$model->setHighestOrderNumber();
}
});

static::updating(function (Model $model): void {
if ($model instanceof Sortable && $model->shouldSortWhenUpdating() && !empty($model->sortables)) {
if (($model instanceof Sortable || $model instanceof Model) && $model->shouldSortWhenUpdating()) {
self::setMassNewOrder($model->sortables);
}
});

static::deleting(function (Model $model): void {
if ($model instanceof Sortable && $model->shouldSortWhenDeleting() && !empty($model->sortables)) {
if (($model instanceof Sortable || $model instanceof Model) && $model->shouldSortWhenDeleting()) {
self::setMassNewOrder($model->sortables);
}
});
Expand Down

0 comments on commit 59bd4a1

Please sign in to comment.