🚀 Original Summary: A Laravel package called Referenceable simplifies the task of generating unique reference numbers for models in various web applications. The package features multiple generation strategies, including random, sequential, and template-based options, and supports high configuration flexibility. It provides a template system, allowing for complex formats, and includes sequential number generation with reset options, validation, and collision handling. The package also supports multi-tenancy and artisan commands for management and maintenance.
✅ Tip: The package can be modified to use a globally configured reference strategy in the config/referenceable.php
file, making it easier to manage references across multiple models.
🚀 Sarcasm Summary:
Generating unique reference numbers in web applications can be a tedious task, especially when dealing with complex models and multi-tenant environments. In this article, we’ll see how Referenceable simplifies this challenge by providing a comprehensive package that offers multiple strategies, high configurability, and powerful tools for managing reference numbers.
With In many web applications, generating unique reference numbers for models is a common requirement.
Whether you’re building:
- an e-commerce platform that needs order numbers,
- an invoicing system requiring invoice references, or
- any application that needs trackable identifiers,
managing reference number generation can quickly become complex.
Referenceable is a Laravel package by Mohamed Said that simplifies this challenge.
It enables the generation of customizable model reference numbers with flexible formats and powerful configuration options.
✨ Main Features
-
Multiple Generation Strategies
Random, sequential, and template-based reference generation. -
Highly Configurable
Define prefixes, suffixes, separators, and more. -
Template System
Use placeholders like{YEAR}
,{MONTH}
,{SEQ}
,{RANDOM}
for complex formats. -
Sequential Numbering
Auto-incrementing sequences with reset options (daily, monthly, yearly). -
Validation & Verification
Built-in reference validation and uniqueness checking. -
Collision Handling
Automatic collision detection and resolution. -
Multi-Tenancy Support
Tenant-aware reference generation. -
Artisan Commands
Comprehensive CLI tools for management and maintenance. -
Performance Optimized
Caching, batch processing, and database transactions.
🚀 Example Usage
1️⃣ Migration
Create a migration with a reference_number
column:
Schema::create('status_letters', function (Blueprint $table) {
$table->id();
$table->string('reference_number')->unique()->index();
// other columns...
$table->timestamps();
});
2️⃣ Model Configuration
Use the HasReference
trait and configure the reference strategy:
use MohamedSaid\Referenceable\Traits\HasReference;
class StatusLetter extends Model
{
use HasReference;
protected $referenceColumn = 'reference_number';
protected $referenceStrategy = 'template';
protected $referenceTemplate = [
'format' => '{PREFIX}-{YEAR}{MONTH}{SEQ}',
'sequence_length' => 4,
];
protected $referencePrefix = 'STL';
// Generates: STL-2025090001, STL-2025090002...
}
🔀 Other Strategies
Random Strategy:
protected $referenceStrategy = 'random';
protected $referencePrefix = 'STL';
protected $referenceLength = 6;
protected $referenceCase = 'upper';
// Generates: STL-A12BC3
Sequential Strategy:
protected $referenceStrategy = 'sequential';
protected $referencePrefix = 'STL';
protected $referenceSequential = [
'start' => 1000,
'min_digits' => 4,
'reset_frequency' => 'monthly', // never, daily, monthly, yearly
];
// Generates: STL-001000, STL-001001, STL-001002...
✅ Tip: You can also configure references globally in config/referenceable.php
instead of per model.
🛠 Useful Methods & Scopes
// Manually generate without saving
$reference = $statusLetter->generateReference();
// Check if a model has a reference
if ($statusLetter->hasReference()) {
echo "Reference Number: " . $statusLetter->reference;
}
// Find a record by reference number
$statusLetter = StatusLetter::findByReference('STL-2025090001');
// Query references starting with a prefix
$lettersThisMonth = StatusLetter::referenceStartsWith('STL-202509')->get();
⚡ Installation
Install via Composer and run the install command:
composer require eg-mohamed/referenceable
php artisan referenceable:install
View the source code on GitHub: github.com/EG-Mohamed/Referenceable