收入/唯一标识

该软件包已被废弃且不再维护。没有推荐替代软件包。

生成64位唯一标识符,并与Laravel Eloquent结合使用。

1.0.0 2021-03-06 18:32 UTC

This package is auto-updated.

Last update: 2023-06-07 08:22:45 UTC


README

生成64位唯一标识符,并与Laravel Eloquent结合使用。

Latest Version on Packagist Software License Build Status Total Downloads

概览

该项目灵感来源于文章 Instagram中的分片与ID。使用它,您可以为您表的uid创建64位长度的唯一标识符,并按时间排序。

安装

使用以下命令通过composer安装此软件包

composer require reishou/unique-identity

Laravel使用包自动发现,因此不需要您手动添加ServiceProvider。

使用以下命令发布配置文件

php artisan vendor:publish --provider="Reishou\UniqueIdentity\UidServiceProvider"

您可以在config/uid.php中更改entity_table名称(默认为entity_sequences)。然后运行生成迁移命令

php artisan uid:table

迁移生成后,您可以通过以下命令创建entity_table

php artisan migrate

使用

在Eloquent启动创建时自动生成

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Reishou\UniqueIdentity\HasUid;

class YourModel extends Model
{
    use HasUid;
}

您的Eloquent模型应使用Reishou\UniqueIdentity\HasUid特质。

// instantiate a new model
$model = new YourModel();
// set attributes on the model
$model->field = $value;
// save model
$model->save();
// or use the create method to "save" a new model
YourModel::create($attributes)

手动生成uid列表

在将多条记录插入数据库之前,您可以生成多个uid。

// $listAttributes contain 10 elements, every element is an array attributes will insert to database.
$listAttributes = [...]; 
// array $ids contains 10 uid
$ids = YourModel::uid(count($listAttributes));
// set ids to attributes
$listAttributes = collect($listAttributes)->map(function ($attributes, $index) use ($ids) {
    $attributes['id']         = $ids[$index];
    $attributes['created_at'] = now();
    $attributes['updated_at'] = now();
    
    return $attributes;
})
    ->toArray();
// insert to database
YourModel::insert($listAttributes);