gowelle / sku-generator
Generate meaningful SKUs for Laravel e-commerce products and variants
Installs: 96
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/gowelle/sku-generator
Requires
- php: ^8.2
- illuminate/support: ^10.0 || ^11.0 || ^12.0
- laravel/prompts: ^0.3.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- spatie/laravel-package-tools: ^1.0
This package is auto-updated.
Last update: 2025-11-14 23:58:22 UTC
README
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
SKU History & Audit Trail
The package includes a comprehensive audit trail system to track all SKU lifecycle events.
Setup
Publish and run the migration:
php artisan vendor:publish --tag="sku-generator-migrations"
php artisan migrate
Configuration
Configure history tracking in config/sku-generator.php:
'history' => [ 'enabled' => env('SKU_HISTORY_ENABLED', true), 'track_user' => true, 'track_ip' => false, 'track_user_agent' => false, 'retention_days' => null, // null = keep forever 'table_name' => 'sku_histories', ],
Tracked Events
The following events are automatically tracked:
- Created: When a new SKU is generated
- Regenerated: When
forceRegenerateSku()is called - Modified: When a SKU is manually modified
- Deleted: When a model with a SKU is deleted
Viewing History
Via Model Relationship
// Get all history for a model $history = $product->skuHistory; // Get latest history entry $latest = $product->getLatestSkuHistory(); // Get all history entries $allHistory = $product->getSkuHistory();
Via Artisan Command
# View history for a specific model php artisan sku:history "App\Models\Product" --id=123 # View history for a specific SKU php artisan sku:history --sku="TM-TSH-ABC12345" # View recent changes php artisan sku:history --recent --days=7 # Filter by event type php artisan sku:history --event=regenerated --limit=100
Query Interface
Use the SkuHistory model to query history:
use Gowelle\SkuGenerator\Models\SkuHistory; // Get history for a specific model SkuHistory::forModel($product)->get(); // Find history for a specific SKU SkuHistory::forSku('TM-TSH-ABC12345')->get(); // Filter by event type SkuHistory::byEventType('regenerated')->get(); // Get recent changes SkuHistory::recentChanges(7)->get(); // Filter by date range SkuHistory::between('2024-01-01', '2024-12-31')->get(); // Filter by user SkuHistory::byUser($userId)->get();
Events
The package dispatches Laravel events for all SKU changes:
use Gowelle\SkuGenerator\Events\{SkuCreated, SkuRegenerated, SkuModified, SkuDeleted}; // Listen to events in your EventServiceProvider Event::listen(SkuRegenerated::class, function ($event) { // $event->model - The model that was changed // $event->oldSku - The previous SKU // $event->newSku - The new SKU // $event->reason - Optional reason for change });
Cleanup Old History
Clean up old history records:
# Cleanup based on configured retention policy php artisan sku:history:cleanup # Delete records older than 365 days php artisan sku:history:cleanup --days=365 # Delete records before a specific date php artisan sku:history:cleanup --before="2024-01-01" # Preview what would be deleted php artisan sku:history:cleanup --days=365 --dry-run # Skip confirmation php artisan sku:history:cleanup --days=365 --force
Disabling History
To disable history tracking:
// In config/sku-generator.php 'history' => [ 'enabled' => false, // ... ], // Or via environment variable SKU_HISTORY_ENABLED=false
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.