riipandi/laravel-optikey
Composer 安装命令:
composer require riipandi/laravel-optikey
包简介
Use UUID, Ulid, or nanoid as optional or primary key in Laravel.
README 文档
README
Use UUID, Ulid, or nanoid as optional or primary key in Laravel.
composer require riipandi/laravel-optikey
This package adds a very simple trait to automatically generate a UUID, Ulid, or nanoid for your Models.
✌️ Using as Secondary Key
1. Update your schemas
First, you need to add an extra column in your migration. For example:
php artisan make:migration AddOptikeyToUsersTable
// If using UUID for the key $table->uuid('uid')->after('id')->unique()->index(); // If using nanoid or ulid for the key $table->string('uid')->after('id')->unique()->index();
Sample migration:
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddOptikeyToUsersTable extends Migration { public function up() { // Add uid column to users table Schema::table('users', function (Blueprint $table) { $table->string('uid', 26)->index()->after('id'); }); // Prefill uid column in users table Schema::table('users', function (Blueprint $table) { $results = DB::table('users')->select('id')->get(); foreach ($results as $result) { $ulid = \Ulid\Ulid::generate($lowercase = true); // Generate new lowercase Ulid $generated = 'user_'.$ulid; // this is the generated value with optional prefix DB::table('users')->where('id', $result->id)->update(['uid' => $generated]); } }); // Set uid column as unique Schema::table('users', function (Blueprint $table) { $table->unique('uid'); }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('uid'); }); } }
2. Add the trait
Add the trait to your model (pick one between HasUuidKey, HasUlidKey, or HasNanoidKey):
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Riipandi\LaravelOptiKey\Traits\HasNanoidKey; class User extends Model { use HasNanoidKey; protected $optiKeyFieldName = 'uid'; // mandatory (you can change this field name) protected $optiKeyLowerCase = true; // optional (default: false) protected $optiKeyPrefix = 'user_'; // optional (default: null) .... }
3. Get Record using the key
Using scope:
\App\User::byOptiKey('xxxxxxxxxxx')->first();
Or, using static find method:
\App\User::findByOptiKey('xxxxxxxxxxx');
☝️ Using as Primary Key
You need to change the primary key field type in your migration. For example:
$table->uuid('id')->primary(); // for UUID $table->string('id', 26)->primary(); // for Ulid $table->string('id', 16)->primary(); // for nanoid
Add second trait to use as primary key:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Riipandi\LaravelOptiKey\Traits\HasUlidKey; use Riipandi\LaravelOptiKey\Traits\OptiKeyAsPrimary; class User extends Model { use HasUlidKey; use OptiKeyAsPrimary; protected $optiKeyFieldName = 'id'; ... }
It simply tells Laravel that your primary key isn't an auto-incrementing integer, so it will treat the value correctly.
📝 Important Note
You can use prefix option to add a prefix to the generated key.
- Default lengt for Ulid is 26 characters.
- Default length for nanoid is 16 characters.
- If you want to use prefix, set larger length.
Licence
This project is licensed under MIT: https://aris.mit-license.org
Copyrights in this project are retained by their contributors. No copyright assignment is required to contribute to this project.
This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Please see license file for more information.
riipandi/laravel-optikey 适用场景与选型建议
riipandi/laravel-optikey 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.32k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2020 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 「laravel」 「ulid」 「nanoid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 riipandi/laravel-optikey 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 riipandi/laravel-optikey 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 riipandi/laravel-optikey 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
A simple drop-in solution for providing UUID and ULID support for the IDs of your Eloquent models.
Small Unique Identifier - Quite like an ULID, but half smaller (64 bits).
Provides Doctrine types for mediagone/small-uid package.
统计信息
- 总下载量: 9.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-24