marjose123/laravel-numberizer

Laravel package to create autonumber for Eloquent model

1.0.01 2023-11-18 16:11 UTC

This package is auto-updated.

Last update: 2024-11-08 03:10:58 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Introduction

Laravel-Numberizer is a fork of laravel-autonumber with a support of latest laravel version.

Installation

You can install the package via composer:

composer require marjose123/laravel-numberizer

You can publish and run the migrations with:

php artisan vendor:publish --tag="numberizer-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="numberizer-config"

This is the contents of the published config file:

return [
 /*
    * '?' will be replaced with the increment number.
    */
    'placeholder' => '?',
    /*
     * The number of digits in the autonumber
     */
    'length' => (int) 6,
    /*
     *   The Starting Value you want to start the creation of the incremental number
     */
    'startingValue' => (int) 11111
];

Usage

  1. Add the AutoNumber contracts and add also to your model the HasNumberizer concerns.
use  \MarJose123\LaravelNumberizer\Contracts\AutoNumber;
use \MarJose123\LaravelNumberizer\Concerns\HasNumberizer;

class Purchase extends Model implements AutoNumber
{
    use HasNumberizer;
    
    /**
     * Return the autonumber configuration array for this model.
     *
     * @return array
     */
    public function getAutoNumberOptions()
    {
        return [
            'purchase_number' => [
                'format' => 'PO-?', // autonumber format. '?' will be replaced with the generated number.
                'length' => 5 // The number of digits in an autonumber
            ]
        ];
    }

}
  1. If you want to use closure on your format you do so.
public function getAutoNumberOptions()
{
    return [
        'purchase_number' => [
            'format' => function () {
                return 'PO-' . \Carbon\Carbon::today()->year . '-?'; // autonumber format. '?' will be replaced with the generated number.
            },
            'length' => 6 // The number of digits in the autonumber
        ]
    ];
}

The purchase_number will be automatically generated based on the format given when saving the Purchase model.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Thank you!

License

The MIT License (MIT). Please see License File for more information.