A
A
Artem00712019-01-11 15:13:52
Laravel
Artem0071, 2019-01-11 15:13:52

How to disable incrementing?

There is a User model:

class User extends Authenticatable
{
    use UUID;
...
}

And there is a trait UUID:
trait UUID
{
    protected static function boot()
    {
        parent::boot();

        static::creating(function (Model $model) {
            $model->setIncrementing(false);
            $model->{$model->getKeyName()} = Str::orderedUuid();
        });
    }
}

It seems that it should add uuid instead of id
At the same time, if immediately after creating $user->getAttribute('id') id is displayed
But if we do this: $user->fresh()->getAttribute('id') - then we will only get the first number from this very uuid...
At the same time, if you remove $model->setIncrementing(false) and add to User class: Then everything will work as it should.. But I want to do it all in a trait, because in each class I have to do this thing
public $incrementing = false;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vism, 2019-01-11
@vism

trait UUID
{
    protected static function boot()
    {
        parent::boot();
            $model->setIncrementing(false);

        static::creating(function (Model $model) {
            $model->{$model->getKeyName()} = Str::orderedUuid();
        });
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question