PHPackages                             binary-cats/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. [Database &amp; ORM](/categories/database)
4. /
5. binary-cats/laravel-sku

ActiveLibrary[Database &amp; ORM](/categories/database)

binary-cats/laravel-sku
=======================

Generate SKUs for Eloquent models

1.0.0(5mo ago)7570.6k↑31.4%11MITPHPPHP ^8.2CI passing

Since Dec 28Pushed 5mo ago5 watchersCompare

[ Source](https://github.com/binary-cats/laravel-sku)[ Packagist](https://packagist.org/packages/binary-cats/laravel-sku)[ Docs](https://github.com/binary-cats/laravel-sku)[ RSS](/packages/binary-cats-laravel-sku/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (10)Dependencies (3)Versions (19)Used By (0)

[![](./art/support-ukraine.svg)](https://supportukrainenow.org)

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

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

Generate unique SKUs when saving any Eloquent model.

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

echo $model->sku; // outputs "LAR-80564492"
```

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

Installation
------------

[](#installation)

Minimum requirements:

- Laravel 12+
- PHP 8.2+

You can install the package via composer:

```
composer require binary-cats/laravel-sku
```

[Upgrading from previous versions](./UPGRADING.md) should be minimal. However, if you extend or replace the base classes, please make sure to review the changes.

For older versions of Laravel, use version 0.8:

```
composer require binary-cats/laravel-sku:"^0.8"
```

The service provider will automatically register itself.

You can publish the config file with:

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

```
namespace App;

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

class Product extends Model
{
    use HasSku;

    /**
     * Get the options for generating the Sku.
     */
    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 BinaryCats\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';
        }
        // Implode 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 implementation completely; just remember that custom generator must implement `BinaryCats\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.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using issue tracker.

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Credits
-------

[](#credits)

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

Support us
----------

[](#support-us)

[Binary Cats](https://binarycats.dev) is a webdesign agency based in Illinois, US.

License
-------

[](#license)

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

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance73

Regular maintenance activity

Popularity45

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 91.5% 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.

###  Release Activity

Cadence

Every ~150 days

Recently: every ~284 days

Total

16

Last Release

153d ago

Major Versions

0.8.0 → 1.0.02026-03-08

PHP version history (3 changes)0.1.0PHP ^7.2

0.4.0PHP ^7.2|^8.0

1.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/ebb48367388b4368b14cca42714bb13002d7414d9dc8da19c5490ef65c059719?d=identicon)[cyrill.kalita@gmail.com](/maintainers/cyrill.kalita@gmail.com)

---

Top Contributors

[![cyrillkalita](https://avatars.githubusercontent.com/u/2401848?v=4)](https://github.com/cyrillkalita "cyrillkalita (43 commits)")[![nickfls](https://avatars.githubusercontent.com/u/12013206?v=4)](https://github.com/nickfls "nickfls (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

laraveleloquentbinary-catssku

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.9M107](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k35.7M51](/packages/kirschbaum-development-eloquent-power-joins)[illuminate/database

The Illuminate Database package.

2.8k55.8M12.8k](/packages/illuminate-database)[spatie/laravel-sluggable

Generate slugs when saving Eloquent models

1.6k13.4M346](/packages/spatie-laravel-sluggable)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)[lemaur/eloquent-publishing

218.5k1](/packages/lemaur-eloquent-publishing)

PHPackages © 2026

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