Easy creation of unique number generator for your Eloquent models in Laravel.
NOTE: These instructions are for the latest version of Laravel.
If you are using an older version, please install a version of the package that correlates to your Laravel version.
Depending on your version of Laravel, you should install a different version of the package.
-
Install the package via Composer:
$ composer require mariuszmalek/unique-number-generator:dev-master
The package will automatically register its service provider.
-
Optionally, publish the configuration file if you want to change any defaults:
php artisan vendor:publish --provider="Malek\UniqueNumberGenerator\ServiceProvider"
Saving a model is easy:
$post->number = GeneratorNumber::generateID(Post::class, 'number', 'CLI-', $params);
The first parameter is $model in which we search an existing class, the second is a specific column in the database.
The next one is the prefix that will be added to the beginning of the result. The last one is the parameters for specific search classes. Only the first two parameters are required
Using traits:
use Malek\UniqueNumberGenerator\Numerable;
class Product extends Model
{
use Numerable;
/**
* @return string
*/
public function uniqueColumn(): string
{
return 'number';
}
}
You can add a Trait, so that after creating the model, a new unique number will be automatically created. You also need to add the uniqueColumn function which will contain the name of the column to be used.
Please use Github for reporting bugs, and making comments or suggestions.
See CONTRIBUTING.md for how to contribute changes.
unique-number-generator was written by Mariusz Malek and is released under the MIT License.
Copyright (c) 2021 Mariusz Malek