eg-mohamed/referenceable
Laravel package that adds reference numbers to Eloquent models with configurable formats. Supports random, sequential, and template-based generation (e.g., year/month/seq/random), collision handling, validation, and tenant-aware sequences. Includes install command, config publishing, and Laravel 10–...
company_id scoping). The package’s referenceUniquenessScope and referenceTenantColumn directly map to our multi-tenant database design.INV-{YEAR}{SEQ} for invoices, TKT-{RANDOM} for support tickets), reducing custom development by 60% for reference logic.referenceLength = 6).reference columns).referenceable:generate for legacy data migration).reference) and optional counter tables for sequential strategies. No breaking changes to existing tables.config/referenceable.php), critical for legacy data migration (e.g., 50K+ orders).| Risk Area | Mitigation Strategy |
|---|---|
| Collision Handling | Package includes referenceCollisionStrategy (retry/fail/append) with configurable maxRetries. Test with edge cases (e.g., concurrent writes). |
| Sequential Gaps | Reset frequency (daily/monthly/yearly) prevents gaps. Monitor model_reference_counters table for anomalies. |
| Multi-Tenancy Leaks | Validate referenceUniquenessScope = 'tenant' and referenceTenantColumn in all models. Use database indexes on tenant + reference columns. |
| Legacy Data Migration | Use referenceable:generate with --batch flag. Test with a subset of data first. |
Format Standardization:
{PREFIX}{YEAR}{SEQ} for all models) or allow model-specific formats?Sequential Strategy:
reset_frequency = 'never' (risk of gaps) or yearly (simpler but resets annually)?Multi-Tenancy:
uniqueness_scope = 'tenant' for all models or require explicit opt-in?Legacy Systems:
Monitoring:
reference column.config/referenceable.php controls TTL). Compatible with Redis/Memcached.referenceable:generate --batch=1000), ideal for background migration tasks.| Phase | Action | Tools/Commands |
|---|---|---|
| 1. Evaluation | Test package with 1–2 pilot models (e.g., Order, Invoice). |
composer require eg-mohamed/referenceable, php artisan referenceable:install |
| 2. Configuration | Define global defaults (config/referenceable.php) and model-specific overrides. |
Update app/Models/Order.php, app/Models/Invoice.php with traits/config. |
| 3. Legacy Migration | Migrate existing data to new format in batches. | php artisan referenceable:generate App\Models\Order --batch=500 --dry-run |
| 4. Rollout | Enable for new models first, then legacy models during maintenance windows. | Update model traits incrementally. |
| 5. Monitoring | Set up logs/alerts for collision retries and generation failures. | Laravel Horizon or custom observer for referenceable events. |
reference column. Existing queries (e.g., WHERE reference = 'INV-123') work unchanged.strtoupper($model->id)) can be gradually replaced by the trait.Activitylog, Laravel Cashier).creating, saving). Test with event listeners.SupportTicket, WebhookLog) to validate the package.Invoice) for gap analysis.generateOrderId() helper methods).php artisan referenceable:regenerate App\Models\Order --id=123).model_reference_counters).referenceMaxRetries and adjust referenceCollisionStrategy.model_reference_counters for unexpected resets.referenceTenantColumn is correctly set.referenceable:stats, referenceable:validate.referenceable event logging for auditing.Order vs. Invoice formats).batch_size in config/referenceable.php (default: 100).cache_config to reduce model configuration lookups.reference and [tenant_column, reference] for large datasets.reset_frequency = 'never' for high-write models (e.g., Orders) to avoid contention.| Scenario | Impact | Mitigation |
|---|---|---|
| Database connection loss | Failed reference generation. | Retry logic with exponential backoff. |
| Collision retries exhausted | Duplicate references. | Switch to collisionStrategy = 'append' or alert support team. |
| Counter table corruption | Sequential gaps or duplicates. | Backup model_reference_counters; restore from snapshot. |
| Multi-tenancy misconfiguration | Cross-tenant reference leaks. | Validate referenceUniquenessScope in CI/CD. |
| Legacy migration failure | Partial data conversion. | Use --dry-run and transactional batches for rollback safety. |
How can I help you explore Laravel packages today?