amidesfahani/laravel-sku

Generate SKUs for Eloquent models

dev-main 2021-10-14 08:32 UTC

This package is auto-updated.

Last update: 2024-11-14 14:56:57 UTC


README

Generate unique SKUs when saving any Eloquent model with support for Laravel 5.6, Laravel 6 and above.

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

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

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

Installation

You can install the package via composer:

composer require amidesfahani/laravel-sku

The service provider will automatically register itself.

You can publish the config file with:

php artisan vendor:publish --provider="Amid\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 or string
         */
        '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' => \Amid\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 Amid\Sku\HasSku trait to your model. That's it!

namespace App;

use Amid\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 Amid\Sku\HasSku;
use Amid\Sku\Concerns\SkuOptions;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasSku;

    /**
     * Get the options for generating the Sku.
     *
     * @return Amid\Sku\SkuOptions
     */
    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 Amid\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';
        }
        // Impode 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 implemetation completely; just remember that custom generator must implement Amid\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.

Changelog

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

Testing

composer test

Credits (Original Package)

License

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