yieldstudio/eloquent-public-id
Composer 安装命令:
composer require yieldstudio/eloquent-public-id
包简介
Eloquent Public ID Trait for Laravel 9 and above.
README 文档
README
What It Does
The interest of public IDs is to keep a whole and incremental ID, while having a UUID to expose to the front end, which can be convenient for security reasons.
This package offers two features:
- Allow models to manage a public ID
- Allow FormRequest to easily convert public IDs to IDs
Installation
You can install the package via composer:
composer require yieldstudio/eloquent-public-id
HasPublicId Trait
This Trait will enable your Model to have benefit of all the actions needed to process the public id.
Once package installed, Add a public id field into your table
Schema::create('users', function (Blueprint $table) { // .. $table->uuid('public_id')->index()->unique(); // .. });
Next step, use the HasPublicId trait into your Model
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use YieldStudio\EloquentPublicId\HasPublicId; class User extends Model { use HasPublicId; }
It's ready to work :)
⚠️ By default the trait will mark the ID field as a hidden field and guard the public ID.
The Trait adds some methods to your Model, here they are:
| Name | Description |
|---|---|
| wherePublicId(string $publicId) | A new scope to find with a public ID |
| findByPublicId(string $publicId, array $columns = ['*']) | A new static method to get a model by their public ID |
| getPublicIdName() | Returns the public ID column name |
| getPublicId() | Returns the public ID of the model |
Change the name of the public ID column
If in your migration you have chosen another field name instead of public_id, you have to specify this field using the getPublicIdName function.
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use YieldStudio\EloquentPublicId\HasPublicId; class User extends Model { use HasPublicId; public function getPublicIdName(): string { return 'uuid'; } }
Change the generation of the public ID
The public id is automatically generated once your Model is created in the database.
If you want to modify the value of the generation of this field, you must add the generatePublicId function to your Model
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use YieldStudio\EloquentPublicId\HasPublicId; class User extends Model { use HasPublicId; public function generatePublicId(): string { return Str::random(); } }
ConvertPublicId Trait
Allowing to convert public IDs to IDs in a FormRequest (before validation).
<?php declare(strict_types=1); namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use YieldStudio\EloquentPublicId\ConvertPublicId; class CreatePostRequest extends FormRequest { use ConvertPublicId; protected array $publicIdsToConvert = [ 'category_id' => Category::class, 'tags.*' => Tag::class, 'postable_id' => 'postable_type', // You can reference another field as model class in case of morph relationship 'suggestions' => [ // Nesting fields is allowed '*' => [ 'post_id' => Post::class, 'tags.*' => Tag::class, 'postable_id' => 'postable_type', ] ] ];
Unit tests
To run the tests, just run composer install and composer test.
Contact us
Our team at Yield Studio is ready to welcome you and make every interaction an exceptional experience. You can contact us.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail contact@yieldstudio.fr instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
yieldstudio/eloquent-public-id 适用场景与选型建议
yieldstudio/eloquent-public-id 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59.64k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「laravel」 「eloquent」 「public id」 「yieldstudio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yieldstudio/eloquent-public-id 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yieldstudio/eloquent-public-id 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yieldstudio/eloquent-public-id 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Provide a way to secure accesses to all routes of an symfony application.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
It's a barebone security class written on PHP
统计信息
- 总下载量: 59.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-02
