PHPackages                             amidesfahani/laravel-sku - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. amidesfahani/laravel-sku

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

amidesfahani/laravel-sku
========================

Generate SKUs for Eloquent models

030PHP

Since Oct 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/amidesfahani/laravel-sku)[ Packagist](https://packagist.org/packages/amidesfahani/laravel-sku)[ RSS](/packages/amidesfahani-laravel-sku/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Handle SKUs for your models
===========================

[](#handle-skus-for-your-models)

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
------------

[](#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

[](#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
--------------

[](#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

[](#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

[](#about-skus)

[Stock Keeping Unit](https://en.wikipedia.org/wiki/Stock_keeping_unit) allows you to set a unique identifier or code that refers to the particular stock keeping unit.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.

Testing
-------

[](#testing)

```
composer test
```

Credits (Original Package)
--------------------------

[](#credits-original-package)

- [Cyrill Kalita](https://github.com/binary-cats)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/5d9ed461878eff3be19c9bfd4158e246a2a02f3728fa9589d8d48b61867fc2b0?d=identicon)[amidesfahani](/maintainers/amidesfahani)

---

Top Contributors

[![amidesfahani](https://avatars.githubusercontent.com/u/61671721?v=4)](https://github.com/amidesfahani "amidesfahani (4 commits)")

### Embed Badge

![Health badge](/badges/amidesfahani-laravel-sku/health.svg)

```
[![Health](https://phpackages.com/badges/amidesfahani-laravel-sku/health.svg)](https://phpackages.com/packages/amidesfahani-laravel-sku)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
