hypermetrica/laravel-sku

Generate SKUs for Eloquent models

dev-master 2021-01-23 19:01 UTC

This package is auto-updated.

Last update: 2024-04-24 02:13:31 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 hypermetrica/laravel-sku

The service provider will automatically register itself.

You can publish the config file with:

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

namespace App;

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

class Product extends Model
{
    use HasSku;

    /**
     * Get the options for generating the Sku.
     *
     * @return Hypermetrica\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);
    }
}

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

Security

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