PHP code example of gowelle / sku-generator

1. Go to this page and download the library: Download gowelle/sku-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gowelle / sku-generator example snippets


use Gowelle\SkuGenerator\Concerns\HasSku;

class Product extends Model
{
    use HasSku;
}

$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

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,
    ],
];

// Change from:
$table->unsignedBigInteger('model_id')->index();
$table->unsignedBigInteger('user_id')->nullable();

// To:
$table->uuid('model_id')->index();
$table->uuid('user_id')->nullable();

// Change from:
$table->unsignedBigInteger('model_id')->index();
$table->unsignedBigInteger('user_id')->nullable();

// To:
$table->string('model_id')->index();
$table->string('user_id')->nullable();

// Example: UUID for models, integer for users
$table->uuid('model_id')->index();
$table->unsignedBigInteger('user_id')->nullable();

'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',
],

// Get all history for a model
$history = $product->skuHistory;

// Get latest history entry
$latest = $product->getLatestSkuHistory();

// Get all history entries
$allHistory = $product->getSkuHistory();

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();

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
});

// In config/sku-generator.php
'history' => [
    'enabled' => false,
    // ...
],

// Or via environment variable
SKU_HISTORY_ENABLED=false
bash
php artisan vendor:publish --tag="sku-generator-config"
bash
# Interactive mode
php artisan sku:regenerate

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

# Skip confirmation
php artisan sku:regenerate --force
bash
php artisan vendor:publish --tag="sku-generator-migrations"
php artisan migrate