Using a UUID as primary key with Laravel 5

后端 未结 4 1535
余生分开走
余生分开走 2021-01-03 07:27

I\'m trying to create tables that will have a primary key which is a UUID defined as binary(16) instead of the default auto-incrementing id field.<

4条回答
  •  轮回少年
    2021-01-03 07:42

    So, I got the thing working like a charm (not tested unit testing):

    1. class UuidModel extends Eloquent is an older (Laravel 4) construct. We use class UuidModel extends Model in Laravel 5
    2. The solution was to move the

      UuidModel::creating(function ($model) {
          echo "Creating Uuid Model...\n";
          $model->{$model->getKeyName()} = (string)$model->generateNewId();
      });
      

      from AppServiceProvider::boot() to EventServiceProvider::boot(). No other changes were required. Everything worked as expected.

    I still don't know why (2) works in EventServiceProvider and not in AppServiceProvider as explained in the official docs. But judging from the name, that's perhaps the way it was meant to be.

提交回复
热议问题