jobsrey/laravel-autonumber is a Laravel package.
It currently has 0 GitHub stars and 98 downloads on Packagist (latest version 2.1.0).
Install it with composer require jobsrey/laravel-autonumber.
Discover more Laravel packages by jobsrey
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel package for automatically generating unique sequential numbers for your Eloquent models.
Install the package via Composer:
composer require jobsrey/laravel-autonumber
The package uses Laravel's auto-discovery feature, so the Service Provider will be automatically registered in Laravel 5.5 and above.
For older Laravel versions or if you need manual registration, add the service provider to config/app.php:
'providers' => [
// Other Service Providers...
Jobsrey\AutoNumber\AutoNumberServiceProvider::class,
],
Publish the configuration file:
php artisan vendor:publish --provider="Jobsrey\AutoNumber\AutoNumberServiceProvider" --tag="config"
This will create a config/autonumber.php file with the following options:
return [
/*
* Autonumber format
* '?' will be replaced with the increment number.
*/
'format' => '?',
/*
* The number of digits in the autonumber
*/
'length' => 4,
/*
* Whether to update the autonumber value when a model is being updated.
* Defaults to false, which means autonumber are not updated.
*/
'onUpdate' => false,
];
Run the migration to create the auto_numbers table:
php artisan vendor:publish --provider="Jobsrey\AutoNumber\AutoNumberServiceProvider" --tag="migrations"
php artisan migrate
Add the AutoNumberTrait to your model and implement the getAutoNumberOptions() method:
use Illuminate\Database\Eloquent\Model;
use Jobsrey\AutoNumber\AutoNumberTrait;
class Invoice extends Model
{
use AutoNumberTrait;
protected $fillable = ['number', 'customer_id', 'amount'];
public function getAutoNumberOptions(): array
{
return [
'number' => [
'format' => 'INV-?', // Format: INV-0001, INV-0002, etc.
'length' => 4, // 4 digits with zero padding
],
];
}
}
Now when you create a new Invoice, the number will be automatically generated:
$invoice = Invoice::create([
'customer_id' => 1,
'amount' => 100.00,
]);
// The number will be automatically set to: INV-0001
echo $invoice->number; // INV-0001
You can define multiple autonumber fields for a single model:
class Order extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'order_number' => [
'format' => 'ORD-?',
'length' => 6,
],
'reference' => [
'format' => 'REF-?',
'length' => 8,
],
];
}
}
You can use a callable for dynamic format generation:
class Ticket extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'ticket_number' => [
'format' => function () {
$prefix = 'TICKET-' . date('Y');
return $prefix . '-?';
},
'length' => 5,
],
];
}
}
You can override the global configuration for specific models:
class Product extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'sku' => [
'format' => 'SKU-?',
'length' => 8, // Override global length
],
];
}
}
By default, autonumbers are only generated when creating new models. To enable autonumber generation on updates:
onUpdate => true in the global config (config/autonumber.php)class Document extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'doc_number' => [
'format' => 'DOC-?',
'length' => 4,
'onUpdate' => true, // Enable update
],
];
}
}
You can separate autonumber counters by group. This is useful when you need different sequences for different categories, branches, companies, or any other grouping criteria.
Use a static string to create a fixed group:
class Invoice extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'number' => [
'format' => 'INV-?',
'length' => 4,
'group' => 'BRANCH-001', // Static group
],
];
}
}
This will generate: INV-0001, INV-0002, etc. for BRANCH-001, and separate sequences for other branches.
Use a model field to create dynamic groups:
class Invoice extends Model
{
use AutoNumberTrait;
protected $fillable = ['number', 'company_id', 'amount'];
public function getAutoNumberOptions(): array
{
return [
'number' => [
'format' => 'INV-?',
'length' => 4,
'group' => $this->company_id, // Group by company
],
];
}
}
Each company will have its own invoice sequence.
Use a callable for complex group logic:
class Invoice extends Model
{
use AutoNumberTrait;
protected $fillable = ['number', 'company_id', 'branch_id', 'amount'];
public function getAutoNumberOptions(): array
{
return [
'number' => [
'format' => 'INV-?',
'length' => 4,
'group' => fn() => $this->company_id . '-' . $this->branch_id,
],
];
}
}
This creates groups like COMPANY-001-BRANCH-A, COMPANY-001-BRANCH-B, etc.
Combine static text with model fields:
class Invoice extends Model
{
use AutoNumberTrait;
protected $fillable = ['number', 'company_id', 'amount'];
public function getAutoNumberOptions(): array
{
return [
'number' => [
'format' => 'INV-?',
'length' => 4,
'group' => $this->company_id . '_test', // Company + suffix
],
];
}
}
Models without a group parameter will continue to work with a global counter:
class LegacyInvoice extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'number' => [
'format' => 'INV-?',
'length' => 4,
// No group parameter - uses global counter
],
];
}
}
You can use different groups for different autonumber fields:
class Order extends Model
{
use AutoNumberTrait;
public function getAutoNumberOptions(): array
{
return [
'order_number' => [
'format' => 'ORD-?',
'length' => 6,
'group' => $this->company_id,
],
'reference' => [
'format' => 'REF-?',
'length' => 8,
'group' => fn() => $this->company_id . '-' . $this->region_id,
],
];
}
}
The pattern for the autonumber. The ? placeholder will be replaced with the increment number.
string or callable'?''INV-?' will generate INV-0001, INV-0002, etc.The number of digits for zero-padding the increment number.
int44 will generate 0001, 0002, etc.Whether to regenerate the autonumber when the model is updated.
boolfalsefalse, autonumbers are only generated on model creationThe group identifier for separating counters per group.
string or callablenull'group' => $this->company_id or 'group' => fn() => $this->company_id . '-' . $this->branch_idnull, the counter is global per model. Each group maintains its own sequence.This package is open-sourced software licensed under the MIT license.
For issues, questions, or contributions, please visit the GitHub repository.