New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

alfa6661/laravel-autonumber

Laravel package to create autonumber for Eloquent model
17,431 47
Install
composer require alfa6661/laravel-autonumber
Latest Version:1.2.5
Laravel:5 – 9
License:MIT
Last Updated:Oct 15, 2023
Links: GitHub  ·  Packagist
Maintainer: alfa6661

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.

Related Packages

wuwx/laravel-autonumber

Laravel package to create autonumber for Eloquent model

2,944 1
yudifaturohman2/autonumber

Laravel package to create autonumber for Eloquent model

1,048 0
sofa/hookable

Laravel Eloquent hooks system.

2,049,184 65
volosyuk/simple-eloquent

Adaptation of eloquent for retrieving its attributes

117,433 55
gzero/eloquent-tree

Eloquent Tree is a tree model for Laravel Eloquent ORM.

59,480 131

Version History

Version Released PHP Laravel License
1.2.5 ^5.1|^6.0|^7.0|^8.0|^9.0 MIT
1.2.4 ^5.1|^6.0|^7.0 MIT
1.2.3 >=5.6.4 || ^7.0 ^5.1|^6.0 MIT
1.2.2 >=5.6.4 || ^7.0 ^5.1|^6.0 MIT
1.2.1 >=5.6.4 || ^7.0 ^5.1|^5.2|^5.3|^5.4 MIT