Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Referenceable

Referenceable Laravel Package

eg-mohamed/referenceable

Laravel package to make Eloquent models referenceable with customizable reference numbers. Supports random, sequential and template-based formats (YEAR/MONTH/SEQ/RANDOM), collision handling, validation, reset rules (daily/monthly/yearly), multi-tenancy, artisan tools, caching and Laravel 10–13.

View on GitHub
Deep Wiki
Context7

Referenceable is a Laravel package that makes your Eloquent models referenceable by generating unique reference numbers automatically. It supports random, sequential, and template-based strategies, letting you produce human-friendly IDs like ORD-2026-000123 or compact random tokens—fully configurable per project.

Built for modern Laravel apps, it includes robust collision handling, validation, and tools for managing sequences and formats, making it ideal for orders, invoices, tickets, and any record needing a stable external identifier.

  • Multiple strategies: random, sequential, and template placeholders ({YEAR}, {MONTH}, {SEQ}, {RANDOM})
  • Flexible formatting: prefixes, suffixes, separators, length, and custom templates
  • Sequential resets: daily/monthly/yearly sequence rollover options
  • Safety built-in: uniqueness checks plus automatic collision detection/resolution
  • Operational tools: artisan commands, caching, and transaction-friendly generation
Frequently asked questions about Referenceable
How do I generate sequential invoice numbers like INV-0001, INV-0002 in Laravel using this package?
Use the sequential strategy by configuring your model with `protected $referenceStrategy = 'sequential'`. Set a prefix (e.g., 'INV') and padding (e.g., 4 digits) in the model or config. The package auto-increments the sequence and handles resets (daily/monthly/yearly) via `reset_rule` in the config.
Can this package handle multi-tenant applications where each tenant needs isolated reference numbers?
Yes, the package supports multi-tenancy by specifying a `tenant_column` in your model’s reference config. It ensures TenantA gets ORD-001 while TenantB gets ORD-001 independently. Requires proper database indexing for performance. Test with high tenant volumes (e.g., 10K+) to validate scalability.
What Laravel versions does this package support, and are there breaking changes between versions?
The package supports Laravel 10–13 with zero breaking changes. Version 2.x maintains backward compatibility with 1.x configs. Run `php artisan referenceable:validate` post-migration to check for deprecated settings. Always update dependencies via Composer for minor version patches.
How does collision handling work if two records try to generate the same reference (e.g., sequential strategy)?
By default, the package uses retry logic to resolve collisions. Configure `collision_strategy` in `config/referenceable.php` to either `retry` (default), `fail`, or `append` (e.g., INV-0001 → INV-0001-1). For high-volume systems, test edge cases with concurrent writes to ensure performance.
Can I use custom templates like {YEAR}/{MONTH}/{SEQ} for references, and how do I validate them?
Yes, use the template strategy with placeholders like `{YEAR}`, `{MONTH}`, `{SEQ}`, or `{RANDOM}`. Validate templates via the `validateReference()` method before deployment. Example: `protected $referenceTemplate = 'ORD-{YEAR}-{SEQ:04}'` generates ORD-2024-0001. The package throws exceptions for invalid syntax.
What’s the best way to migrate an existing Laravel app to use this package without downtime?
Start by auditing models needing references. Add the `reference` column to migrations, then apply the `HasReference` trait incrementally. Use `php artisan referenceable:install` to set up counters. For backfilling existing records, batch-process references with `batch_size` config to avoid DB locks. Test in staging first.
Does this package work with PostgreSQL, or is it MySQL-only?
The package supports MySQL, PostgreSQL, and SQLite out of the box. No database-specific hacks are required. Ensure your `reference` column is a string type with a unique index. For PostgreSQL, test sequential strategies under high concurrency to validate auto-increment behavior.
How can I optimize performance for bulk reference generation (e.g., 10,000+ records)?
Enable caching with `cache_config: true` in the config and set `batch_size: 100` to reduce DB transactions. For sequential strategies, index the `reference_counters` table. Avoid generating references in loops; use batch inserts or queue jobs for large datasets. Monitor DB locks during bulk operations.
Are references immutable, or can I regenerate them if a mistake is made (e.g., wrong prefix)?
References are immutable by default to maintain audit trails. To regenerate, use the `regenerateReference()` method on the model, but note this may violate uniqueness constraints. For corrections, consider adding a `reference_version` column or using soft deletes. Always back up data before mass updates.
What are the alternatives to this package, and why should I choose Referenceable?
Alternatives include `laravel-uuid` (for UUIDs) or custom solutions with `DB::raw()`. Referenceable stands out for its **template flexibility**, **multi-tenancy**, **sequential strategies**, and **collision handling**—all in a single package. It’s ideal for human-readable IDs (e.g., invoices) while avoiding the complexity of UUIDs or manual logic.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport