wuwx/laravel-autonumber

Laravel package to create autonumber for Eloquent model
2,913 1
Install
composer require wuwx/laravel-autonumber
Latest Version:v5.0.0
PHP:^7.0|^8.0
License:MIT
Last Updated:Apr 7, 2026
Links: GitHub  ·  Packagist
Maintainer: wuwx

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 wuwx/laravel-autonumber

Register the ServiceProvider in config/app.php

'providers' => [
    // ...
    Wuwx\LaravelAutoNumber\AutoNumberServiceProvider::class,
],

Publish the default configuration

php artisan vendor:publish --provider='Wuwx\LaravelAutoNumber\AutoNumberServiceProvider'

Running migration

php artisan migrate

Usage

Your Eloquent models should use the Wuwx\LaravelAutoNumber\AutoNumberTrait trait

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

use Wuwx\LaravelAutoNumber\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.

Related Packages

yudifaturohman2/autonumber

Laravel package to create autonumber for Eloquent model

1,048 0
alfa6661/laravel-autonumber

Laravel package to create autonumber for Eloquent model

16,948 47
spatie/laravel-sluggable

Generate slugs when saving Eloquent models

14,760,802 1,557
hootlex/laravel-friendships

This package gives Eloquent models the ability to manage their friendships.

128,287 698
kodeine/laravel-acl

Light-weight role-based permissions for Laravel 5 built in Auth system.

365,109 775

Version History

Version Released PHP Laravel License
v5.0.0 ^7.0|^8.0 ^6.20|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0 MIT
v4.0.0 >=5.6.4 || ^7.0 || ^8.0 ^6.20|^7.0|^8.0 MIT
v3.1.1 >=5.6.4 || ^7.0 ^5.0|^6.0 MIT
v3.0.0 >=5.6.4 || ^7.0 ^5.1|^5.2|^5.3|^5.4|^6.0 MIT
v2.0.0 >=5.6.4 || ^7.0 ^5.1|^5.2|^5.3|^5.4 MIT