承接 cndrsdrmn/eloquent-unique-attributes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

cndrsdrmn/eloquent-unique-attributes

Composer 安装命令:

composer require cndrsdrmn/eloquent-unique-attributes

包简介

A Laravel package to enforce automatic generation of unique attribute values for Eloquent models.

README 文档

README

Packagist Version GitHub Actions Workflow Status GitHub License

A Laravel package to enforce automatic generation of unique attribute values for Eloquent models, making it easy to maintain uniqueness without manual intervention.

🌟 Features

  • Automatically generate unique attribute values for Eloquent models.
  • Customizable formats (numerify, lexify, or mixed patterns).
  • Flexible configuration for prefixes, suffixes, separators, and retries.
  • Supports models with soft deletes.
  • Fully customizable events for enforcing and notifying unique attribute changes.

📖 Background

Managing unique attributes in Eloquent models often involves tedious logic, especially when combining custom formats and real-time checks. This package provides an automatic, extensible solution. It ensures attributes are unique within the database and adhere to defined configurations.

🛠 Installation

Install the package using Composer:

composer require cndrsdrmn/eloquent-unique-attributes

⚙️ Configuration

To customize the default behavior, publish the configuration file:

php artisan vendor:publish --tag=unique-attributes-config

This will create a config/unique_attributes.php file:

return [

    /**
     * The prefix to add to the beginning of the generated unique value.
     */
    'prefix' => '',

    /**
     * The suffix to add to the end of the generated unique value.
     */
    'suffix' => '',

    /**
     * The separator to use between prefix, value, and suffix.
     */
    'separator' => '_',

    /**
     * The maximum number of retries to generate a unique value before increasing the length.
     */
    'max_retries' => 100,

    /**
     * The default length of the generated unique value.
     */
    'length' => 8,

    /**
     * The format of the unique value.
     *
     * Supported options:
     * - `numerify` - Replace `#` with random digits.
     * - `lexify` - Replace `?` with random letters.
     * - `bothify` - Replace `#` with digits and `?` with letters.
     *
     * @see https://github.com/cndrsdrmn/php-string-formatter
     */
    'format' => 'bothify',
];

Configurable Options:

  • prefix: A string prepended to the generated value.
  • suffix: A string appended to the generated value.
  • separator: A separator between prefix, value, and suffix.
  • max_retries: Maximum attempts to generate a unique value before increasing length.
  • length: The initial length of the generated unique value.
  • format: Determines the type of value (numerify for digits, lexify for letters, botify for mixed).

🚀 Usage

1. Add the Trait to Your Model

Include the HasEnforcesUniqueAttributes trait in any Eloquent model requiring unique attributes:

namespace App\Models;

use Cndrsdrmn\EloquentUniqueAttributes\HasEnforcesUniqueAttributes;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasEnforcesUniqueAttributes;

    /**
     * Define the unique attributes configuration.
     */
    public function enforcedUniqueColumns(): array
    {
        return [
            'barcode' => ['format' => 'numerify', 'separator' => '-'],
            'sku' => ['prefix' => 'SKU', 'length' => 10],
        ];
    }
}

2. Generate Unique Attributes

When creating the model, the unique values will automatically be enforced:

$product = Product::create([
    'name' => 'Sample Product',
]);

echo $product->sku; // Example: SKU_1234567890

🔧 Advanced Customization

Event Hooks

This package fires events during the unique value generation process:

  • eloquent.enforcing: Fired before generating unique attributes. If a listener returns a value, the process of enforcing unique attributes is skipped.
  • eloquent.enforced: After unique attributes are generated.

You can listen to these events in your application to implement custom logic:

use Illuminate\Support\Facades\Event;

Event::listen('eloquent.enforcing: App\Models\Product', function ($model, $event) {
    // Returning any value here will skip the enforcement of unique attributes.
    return true;
});

Event::listen('eloquent.enforced: App\Models\Product', function ($model, $status) {
    // Execute logic after unique attributes are enforced.
    if ($status) {
        Log::info('Unique attributes enforced for model', ['model' => $model]);
    }
});

Note: If a listener for the eloquent.enforcing event returns a value (truthy or falsy), the unique attribute generation process will be skipped for the current model.

🧪 Testing

This package includes PHPUnit tests. To run the tests, use the following command:

composer test

🤝 Contributing

We welcome contributions! Follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add new feature").
  4. Push to the branch (git push origin feature-name).
  5. Open a Pull Request.

📄 License

This package is open-source software licensed under the MIT license.

cndrsdrmn/eloquent-unique-attributes 适用场景与选型建议

cndrsdrmn/eloquent-unique-attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 320 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 320
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-28