jetcod/eloquent-keygen 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jetcod/eloquent-keygen

Composer 安装命令:

composer require jetcod/eloquent-keygen

包简介

A Laravel package providing seamless integration with Snowflake ID generation for Eloquent models

README 文档

README

This is a Laravel package providing seamless integration with Snowflake ID generation for Eloquent models. Simplify primary key management with automatic generation using a highly distributed unique identifier system. Enhance your application's scalability and efficiency with this easy-to-use package for Laravel Eloquent models.

Installation

To install jetcod/deloquent-keygen, you can use Composer, the dependency manager for PHP. Run the following command in your terminal:

composer require jetcod/eloquent-keygen

Then you need to publish the config file:

php artisan vendor:publish --provider="eloquent-key-generator-config"

How to use?

To use the package, follow these steps:

  • Extend Your Model:

Instead of extending Illuminate\Database\Eloquent\Model, extend Jetcod\Eloquent\Model in your Eloquent model. This will enable automatic ID generation using the Snowflake algorithm.

<?php

namespace App\Models;

use Jetcod\Eloquent\Model as EloquentModel;

class YourModel extends EloquentModel
{
    // Your model code here
}
  • Using Trait:

If you don't want to extend the Eloquent model, or you want to apply the Snowflake ID generation to one model, you can use the Snowflake trait in your model class.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Jetcod\Eloquent\Traits\Snowflake;

class YourModel extends EloquentModel
{
    use Snowflake;
}
  • Disable Snowflake Generation for a Model:

If you want to disable the Snowflake ID generation for a particular model, you can add a snowflake method to that model returning false.

<?php

namespace App\Models;

use Jetcod\Eloquent\Model as EloquentModel;

class AnotherModel extends EloquentModel
{
    protected function snowflake(): bool
    {
        return false;
    }

    // Your model code here
}

The snowflake method returns a boolean value indicating whether the Snowflake ID generation should be enabled or disabled for that model.

  • Configuration for distributed system:

The package uses the following configuration to generate the distributed unique identifier:

<?php

use Godruoyi\Snowflake\LaravelSequenceResolver;

return [
    'attributes' => [
        /*
        |------------------------------------------------------------------
        | This represents the datacenter id used to generate snowflake ids.
        |------------------------------------------------------------------
        */

        'datacenter' => env('SNOWFLAKE_DATACENTER_ID', 1),

        /*
        |--------------------------------------------------------------
        | This represents the worker id used to generate snowflake ids.
        |--------------------------------------------------------------
        */

        'worker' => env('SNOWFLAKE_WORKER_ID', 1),

        /*
        |--------------------------------------------------------------------------------------------------------
        | This represents the sequence resolver class. It is to ensure that sequence-number generated in the same
        | millisecond of the same node is unique.
        |
        | Avaialable resolvers:
        |   1. Godruoyi\Snowflake\RandomSequenceResolver
        |   2. Godruoyi\Snowflake\FileLockResolver
        |   3. Godruoyi\Snowflake\LaravelSequenceResolver
        |--------------------------------------------------------------------------------------------------------
        */

        'sequence_resolver' => env('SNOWFLAKE_SEQUENCE_RESOLVER', LaravelSequenceResolver::class),

        'file_lock_directory' => env('SNOWFLAKE_File_LOCK_DIRECTORY', null),  // Default is null, means use <app_path>/storage/snowflake directory.
    ],
];

License

This project is licensed under the MIT License - see the LICENSE file for details.

jetcod/eloquent-keygen 适用场景与选型建议

jetcod/eloquent-keygen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.2k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「model」 「laravel」 「Unique ID」 「eloquent」 「primary key」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jetcod/eloquent-keygen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jetcod/eloquent-keygen 我们能提供哪些服务?
定制开发 / 二次开发

基于 jetcod/eloquent-keygen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 39
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-16