src/Database/Models/Traits/SortableClosure.php
/**
     * Adds position to model on creating event.
     */
Adds position to model on creating event.
Adds position to model on creating event .
public static function bootSortableClosure()
    {
        static::addGlobalScope('position', function (Builder $builder) {
            $builder->orderBy((new static)->getPositionColumn());
        });
        static::saving(function ($model) {
            $sortableField = (new static)->getPositionColumn();
            if ($model->$sortableField === null) {
                $model->setAttribute($sortableField, $model->getNextAfterLastPosition($model->parent_id));
            }
        });
        static::deleting(function ($model) {
            $model->siblings(static::QUERY_NEXT_ALL)->decrement((new static)->getPositionColumn());
        });
    }
public static function bootSortableClosure ( ) { static :: addGlobalScope ( 'position' , function ( Builder $ builder ) { $ builder -> orderBy ( ( new static ) -> getPositionColumn ( ) ) ; } ) ; static :: saving ( function ( $ model ) { $ sortableField = ( new static ) -> getPositionColumn ( ) ; if ( $ model -> $ sortableField === null ) { $ model -> setAttribute ( $ sortableField , $ model -> getNextAfterLastPosition ( $ model -> parent_id ) ) ; } } ) ; static :: deleting ( function ( $ model ) { $ model -> siblings ( static :: QUERY_NEXT_ALL ) -> decrement ( ( new static ) -> getPositionColumn ( ) ) ; } ) ; }
Adds position to model on creating event.
Adds position to model on creating event .
/* @var Model $model */ // only automatically calculate next position with max+1 when a position has not been set already