gowelle/sku-generator

Generate meaningful SKUs for Laravel e-commerce products and variants

v1.1.3 2025-05-14 06:56 UTC

This package is auto-updated.

Last update: 2025-05-14 06:59:28 UTC


README

Latest Version on Packagist Total Downloads Tests

Generate meaningful SKUs for Laravel e-commerce products and variants.

Requirements

  • PHP ^8.2
  • Laravel ^10.0|^11.0|^12.0

Installation

You can install the package via composer:

composer require gowelle/sku-generator

Publish the configuration:

php artisan vendor:publish --tag="sku-generator-config"

Usage

Add the HasSku trait to your models:

use Gowelle\SkuGenerator\Concerns\HasSku;

class Product extends Model
{
    use HasSku;
}

SKUs will be automatically generated when models are created:

$product = Product::create(['name' => 'T-Shirt']);
echo $product->sku; // Output: TM-TSH-ABC12345

$variant = $product->variants()->create([/* ... */]);
echo $variant->sku; // Output: TM-TSH-ABC12345-RED-LRG

Configuration

return [
    'prefix' => 'TM',
    'ulid_length' => 8,
    'separator' => '-',

    'models' => [
        \App\Models\Product::class => 'product',
        \App\Models\ProductVariant::class => 'variant',
    ],

    'category' => [
        'accessor' => 'category',
        'field' => 'name',
        'length' => 3,
        'has_many' => false,
    ],
];

SKU Format

Products

  • Format: {prefix}-{category}-{unique}
  • Example: TM-TSH-ABC12345

Variants

  • Format: {prefix}-{category}-{unique}-{properties}
  • Example: TM-TSH-ABC12345-RED-LRG

Regenerating SKUs

Use the artisan command to regenerate SKUs:

# Interactive mode
php artisan sku:regenerate

# Direct model specification
php artisan sku:regenerate "App\Models\Product"

# Skip confirmation
php artisan sku:regenerate --force

Features:

  • Interactive model selection
  • Progress reporting
  • Chunked processing
  • Failure logging
  • Unique constraint preservation

Testing

composer test

Format code:

composer format

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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