alfa6661 / laravel-autonumber by alfa6661

Laravel package to create autonumber for Eloquent model
14,295
45
2
Package Data
Maintainer Username: alfa6661
Maintainer Contact: alfa2159@gmail.com (Alfa Adhitya)
Package Create Date: 2017-08-03
Package Last Update: 2023-10-15
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:07:12
Package Statistics
Total Downloads: 14,295
Monthly Downloads: 104
Daily Downloads: 4
Total Stars: 45
Total Watchers: 2
Total Forks: 20
Total Open Issues: 4

Laravel AutoNumber

Latest Stable Version Total Downloads StyleCI License

Laravel package to create autonumber for Eloquent model

Installation

You can install the package via composer:

composer require alfa6661/laravel-autonumber

Register the ServiceProvider in config/app.php

'providers' => [
    // ...
    Alfa6661\AutoNumber\AutoNumberServiceProvider::class,
],

Publish the default configuration

php artisan vendor:publish --provider='Alfa6661\AutoNumber\AutoNumberServiceProvider'

Running migration

php artisan migrate

Usage

Your Eloquent models should use the Alfa6661\AutoNumber\AutoNumberTrait trait

The trait contains an abstract method getAutoNumberOptions() that you must implement yourself.

use Alfa6661\AutoNumber\AutoNumberTrait;
    
class Order extends Model
{
    use AutoNumberTrait;
    
    /**
     * Return the autonumber configuration array for this model.
     *
     * @return array
     */
    public function getAutoNumberOptions()
    {
        return [
            'order_number' => [
                'format' => 'SO.?', // autonumber format. '?' will be replaced with the generated number.
                'length' => 5 // The number of digits in an autonumber
            ]
        ];
    }

}

You can also pass a closure for the format value.

public function getAutoNumberOptions()
{
    return [
        'order_number' => [
            'format' => function () {
                return 'SO/' . date('Ymd') . '/?'; // autonumber format. '?' will be replaced with the generated number.
            }
            'length' => 5 // The number of digits in the autonumber
        ]
    ];
}

Saving Model

$order = Order::create([
    'customer' => 'Mr. X',
]);

The order_number will be automatically generated based on the format given when saving the Order model.

echo $order->order_number;

// SO/20170803/00001

License

Laravel-autonumber is open-sourced software licensed under the MIT license.

Contributing

Please report any issue you find in the issues page. Pull requests are more than welcome.