HasSnowflakePrimary.php 343 B

12345678910111213141516171819
  1. <?php
  2. namespace App;
  3. use App\Services\SnowflakeService;
  4. trait HasSnowflakePrimary
  5. {
  6. public static function bootHasSnowflakePrimary()
  7. {
  8. static::saving(function ($model) {
  9. if (is_null($model->getKey())) {
  10. $keyName = $model->getKeyName();
  11. $id = SnowflakeService::next();
  12. $model->setAttribute($keyName, $id);
  13. }
  14. });
  15. }
  16. }