bernardomacedo / laravel-skuable
Generate product SKU codes for eloquent models.
Requires
- php: >=7.0.0
- illuminate/database: ~5.5.0|~5.6.0
- illuminate/support: ~5.5.0|~5.6.0
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0
- phpunit/phpunit: ^6.3|^7.0
README
This package provides a trait that will generate a unique Sku when saving any Eloquent model.
$model = new EloquentModel(); $model->name = 'activerecord is awesome'; $model->save(); echo $model->Sku; // ouputs "ACT-7655677"
The Skus are generated with Laravels Str::Sku
method, whereby spaces are converted to '-'.
Bernardomacedo is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Installation
You can install the package via composer:
composer require bernardomacedo/laravel-skuable
Usage
Your Eloquent models should use the Bernardomacedo\Skuable\HasSku
trait and the Bernardomacedo\Skuable\SkuOptions
class.
The trait contains an abstract method getSkuOptions()
that you must implement yourself.
Here's an example of how to implement the trait:
<?php namespace App; use Bernardomacedo\Skuable\HasSku; use Bernardomacedo\Skuable\SkuOptions; use Illuminate\Database\Eloquent\Model; class YourEloquentModel extends Model { use HasSku; /** * Get the options for generating the Sku. */ public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku'); } }
Want to use multiple field as the basis for a Sku? No problem!
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom(['first_name', 'last_name']) ->saveSkusTo('Sku'); }
You can also pass a callable
to generateSkusFrom
.
By default the package will generate unique Skus by appending '-' and a number, to a Sku that already exists.
You can disable this behaviour by calling allowDuplicateSkus
.
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku') ->allowDuplicateSkus(); }
You can also put a maximum size limit on the created Sku:
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku') ->SkusShouldBeNoLongerThan(50); }
You can also use a custom separator by calling usingSeparator
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku') ->usingSeparator('_'); }
To set the language used by Str::Sku
you may call usingLanguage
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku') ->usingLanguage('nl'); }
The Sku may be slightly longer than the value specified, due to the suffix which is added to make it unique.
You can also override the generated Sku just by setting it to another value then the generated Sku.
$model = EloquentModel:create(['name' => 'my name']); //Sku is now "my-name"; $model->Sku = 'my-custom-url'; $model-save(); //Sku is now "my-custom-url";
If you don't want to create the Sku when the model is initially created you can set use the `doNotGenerateSkusOnCreate() function.
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku') ->doNotGenerateSkusOnCreate(); }
Similarly, if you want to prevent the Sku from being updated on model updates, call doNotGenerateSkusOnUpdate()
.
public function getSkuOptions() : SkuOptions { return SkuOptions::create() ->generateSkusFrom('name') ->saveSkusTo('Sku') ->doNotGenerateSkusOnUpdate(); }
This can be helpful for creating permalinks that don't change until you explicitly want it to.
$model = EloquentModel:create(['name' => 'my name']); //Sku is now "my-name"; $model-save(); $model->name = 'changed name'; $model->save(); //Sku stays "my-name"
If you want to explicitly update the Sku on the model you can call generateSku()
on your model at any time to make the Sku according to your other options. Don't forget to save()
the model to persist the update to your database.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email bernardo.macedo@gmail.com instead of using the issue tracker.
Credits
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
License
The MIT License (MIT). Please see License File for more information.