binary-cats/laravel-sku 问题修复 & 功能扩展

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

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

binary-cats/laravel-sku

Composer 安装命令:

composer require binary-cats/laravel-sku

包简介

Generate SKUs for Eloquent models

README 文档

README

Handle SKUs for your models

Generate unique SKUs when saving any Eloquent model.

$model = new EloquentModel();
$model->name = 'Laravel is Awesome';
$model->save();

echo $model->sku; // outputs "LAR-80564492"

Package will add a new method to Laravel's Illuminate\Support\Str::sku() class to generate an SKU for you.

Installation

Minimum requirements:

  • Laravel 12+
  • PHP 8.2+

You can install the package via composer:

composer require binary-cats/laravel-sku

Upgrading from previous versions should be minimal. However, if you extend or replace the base classes, please make sure to review the changes.

For older versions of Laravel, use version 0.8:

composer require binary-cats/laravel-sku:"^0.8"

The service provider will automatically register itself.

You can publish the config file with:

php artisan vendor:publish --provider="BinaryCats\Sku\SkuServiceProvider" --tag="config"

This is the contents of the config file that will be published at config/laravel-sku.php:

return [

    /*
    |--------------------------------------------------------------------------
    | SKU settings
    |--------------------------------------------------------------------------
    |
    | Set up your SKU
    |
    */
    'default' => [
        /*
         * SKU is based on a specific field of a model
         * You can use a single field or an array of fields
         */
        'source' => 'name',

        /*
         * Destination model field name
         *
         */
        'field' => 'sku',

        /*
         * SKU separator
         *
         */
        'separator' => '-',

        /*
         * Shall SKUs be enforced to be unique
         *
         */
        'unique' => true,

        /*
         * Shall SKUs be generated on create
         *
         */
        'generate_on_create' => true,

        /*
         * Shall SKUs be re-generated on update
         *
         */
        'generate_on_update' => true,
    ],

    /*
    |--------------------------------------------------------------------------
    | SKU Generator
    |--------------------------------------------------------------------------
    |
    | Define your own generator if needed.
    |
    */
    'generator' => \BinaryCats\Sku\Concerns\SkuGenerator::class,
];

Please note that the above set up expects you have an sku field in your model. If you plan to manually overwrite the values, please make sure to add this field to fillable array;

Usage

Add BinaryCats\Sku\HasSku trait to your model. That's it!

namespace App;

use BinaryCats\Sku\HasSku;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasSku;
}

Behind the scenes this will register an observer for the sku field, which will be generated every time you save the model.

Advanced usage

If you want to change settings for a specific model, you can overload the skuOptions() method:

namespace App;

use BinaryCats\Sku\HasSku;
use BinaryCats\Sku\Concerns\SkuOptions;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasSku;

    /**
     * Get the options for generating the Sku.
     */
    public function skuOptions(): SkuOptions
    {
        return SkuOptions::make()
            ->from(['label', 'another_field'])
            ->target('arbitrary_sku_field_name')
            ->using('_')
            ->forceUnique(false)
            ->generateOnCreate(true)
            ->refreshOnUpdate(false);
    }
}

Custom Generator

Assuming you want some extra logic, like having a default value, or defining prefix for an SKU, you can implement your own SkuGenerator. It is easiest to extend the base class, but you are free to explore any which way.

First, create a custom class:

namespace App\Components\SkuGenerator;

use BinaryCats\Sku\Concerns\SkuGenerator;

class CustomSkuGenerator extends SkuGenerator
{
    /**
     * Get the source fields for the SKU.
     *
     * @return string
     */
    protected function getSourceString(): string
    {
        // fetch the source fields
        $source = $this->options->source;
        // Fetch fields from model, skip empty
        $fields = array_filter($this->model->only($source));
        // Fetch fields from the model, if empty, use custom logic to resolve
        if (empty($fields)) {
            return 'some-random-value-logic';
        }
        // Implode with a separator
        return implode($this->options->separator, $fields);
    }
}

and then update generator config value:

    'generator' => \App\Components\SkuGenerator\CustomSkuGenerator::class,

You can also opt out to change implementation completely; just remember that custom generator must implement BinaryCats\Sku\Contracts\SkuGenerator.

About SKUs

Stock Keeping Unit allows you to set a unique identifier or code that refers to the particular stock keeping unit.

Testing

composer test

Changelog

Please see CHANGELOG for more information about what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email cyrill.kalita@gmail.com instead of using issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Credits

Support us

Binary Cats is a webdesign agency based in Illinois, US.

License

The MIT License (MIT). Please see License File for more information.

binary-cats/laravel-sku 适用场景与选型建议

binary-cats/laravel-sku 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70.39k 次下载、GitHub Stars 达 75, 最近一次更新时间为 2019 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 binary-cats/laravel-sku 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 75
  • Watchers: 5
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-28