【问题标题】:Laravel UUID generationLaravel UUID 生成
【发布时间】:2016-10-23 06:11:44
【问题描述】:

我正在尝试使用 laravel-uuid 包生成 UUID(不是作为主键,只生成一个)。文档非常简单,因此根据自述文件,只需调用 $uuid = Uuid::generate(); 即可生成 UUID,但它返回一个空对象。 (我也试过$uuid = Uuid::generate(1);

我按照那里提供的安装说明进行操作(没有什么异常),该应用程序没有抛出任何错误,所以我想一切都是正确的。

也欢迎为此提供替代包。

【问题讨论】:

  • 试试echo Uuid::generate()$uuid = Uuid::generate(); echo $uuid->string
  • 我赞同@BenSwinburne 所说的话:查看source 似乎您应该能够通过echo Uuid::generate()->time 取回v1 UUID。或->string 获取字符串版本。
  • 让它工作......结果如果我尝试用响应返回对象,则返回空对象,当我使用 $uuid->string 时它工作。谢谢!

标签: php laravel uuid


【解决方案1】:
  1. 在表名'Uuid'中添加列,输入'char'长度35

  2. 在 Models 文件夹中创建文件夹名称“Traits”

  3. 在 Traits 文件夹中创建文件名 Uuid.php

Uuid.php


    namespace App\Models\Traits;

use Ramsey\Uuid\Uuid as PackageUuid;

trait Uuid
{

    public function scopeUuid($query, $uuid)
    {
        return $query->where($this->getUuidName(), $uuid);
    }

    public function getUuidName()
    {
        return property_exists($this, 'uuidName') ? $this->uuidName : 'uuid';
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->{$model->getUuidName()} = PackageUuid::uuid4()->toString();
        });
    }
}

  1. 在模型中添加“使用 Uuid”

【讨论】:

    【解决方案2】:

    laravel 5.6 之后,添加了一个新的帮助器来生成通用唯一标识符 (UUID)

    use Illuminate\Support\Str;
    
    return (string) Str::uuid();
    
    return (string) Str::orderedUuid();
    

    这些方法返回一个Ramsey\Uuid\Uuid 对象

    orderedUuid() 方法将生成时间戳优先 UUID,以便更轻松、更高效地进行数据库索引。

    【讨论】:

    • 对 laravel 7 也有效。谢谢
    • 也适用于 Laravel 8。
    【解决方案3】:

    在 Laravel 5.6+ 中

    use Illuminate\Support\Str;
    
    $uuid = Str::uuid()->toString();
    

    【讨论】:

      【解决方案4】:

      尝试使用这个包会在你的模型中自动生成和分配 UUID 字段,也可以通过 UUIDs 键显示和更新。

      https://github.com/EmadAdly/laravel-uuid

      【讨论】:

        【解决方案5】:

        原来我必须使用 $uuid->string 来获取实际 ID,如果您尝试在 json 响应中返回它,整个对象将显示为空。

        【讨论】:

          【解决方案6】:

          $uuid 可能是空的,因为您的系统没有提供正确的熵。您可以为 v4 或 v5 UUID 尝试这些库实现:

          // https://tools.ietf.org/html/rfc4122#section-4.4
          function v4() {
              $data = openssl_random_pseudo_bytes(16, $secure);
              if (false === $data) { return false; }
              $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
              $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
              return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
          }
          
          // https://tools.ietf.org/html/rfc4122#section-4.3
          function v5($name) {
              $hash = sha1($name, false);
              return sprintf(
                  '%s-%s-5%s-%s-%s',
                  substr($hash,  0,  8),
                  substr($hash,  8,  4),
                  substr($hash, 17,  3),
                  substr($hash, 24,  4),
                  substr($hash, 32, 12)
              );
          }
          

          【讨论】:

          • 谢谢队友,我会试试这个作为最后的手段,我想使用为 laravel 构建的东西
          猜你喜欢
          • 2017-06-25
          • 2013-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-04
          • 2016-02-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多