gurgentil / laravel-eloquent-sequencer

A package that allows you to create and manage sequences on Eloquent models.
MIT License
156 stars 13 forks source link

Exceed bounds config option #12

Closed dillingham closed 4 years ago

dillingham commented 4 years ago

This PR adds a config option of exceed_bounds => TRUE | FALSE

The goal is to allow the trait to exceed the bounds of the min & max.

This allows package users to handle min / max scenarios on their own like: #8

Without this option, the exception will overthrow any attempts to handle it.

For example, with your explanation / help, was able to set min max values like so:

static::saving(function($model) {
    if($model->isDirty('order')) {
        $next = $model->getLastSequenceValue();
        if($model->order > $next) {
            $model->order = $next;
        } elseif($model->order <= 0) {
            $model->order = 1;
        }
    }
});

This only works if Im able to turn off the exception though. 🙏

Would be awesome if something like that was configurable also.

I can put -1 or 100000 and it will go to either 1 or the max number of models.


Ps: Also defaulted it to false incase anyone published the config so the update wont affect them

config('eloquentsequencer.exceed_bounds', false);