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 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

Technical Evaluation

Architecture Fit

  • High Fit for Laravel Ecosystem: The package is purpose-built for Laravel (v10–13) and leverages Laravel’s Eloquent ORM, migrations, and service providers. It integrates seamlessly with existing Laravel applications, particularly those requiring domain-specific reference IDs (e.g., invoices, orders, tickets).
  • Modular Design: The package follows a trait-based approach (HasReference), allowing granular adoption per model without monolithic changes. This aligns well with Laravel’s modular architecture.
  • Configuration-Driven: Supports global defaults (via config/referenceable.php) and model-specific overrides, enabling consistent yet flexible reference generation across the application.
  • Multi-Tenancy Ready: Built-in support for tenant-aware uniqueness (e.g., company_id), making it ideal for SaaS platforms or partitioned databases.

Integration Feasibility

  • Low-Coupling: The package requires minimal changes to existing models—only the addition of a trait and a reference column. No core Laravel files are modified.
  • Database Schema: Introduces a counter table (model_reference_counters) for sequential strategies, which must be accounted for in migrations. Existing applications can adapt this with minimal effort.
  • Backward Compatibility: Supports v1.x migration paths, reducing risk for incremental adoption.
  • Performance Considerations:
    • Caching: Configurable caching for model configurations and reference generation (reduces DB load).
    • Transactions: Supports batch operations with transactions to ensure data integrity.
    • Indexing: Requires proper indexing on the reference column (and tenant columns for multi-tenancy) for optimal query performance.

Technical Risk

Risk Area Assessment Mitigation
Collision Handling Random strategies may require retries (configurable via referenceMaxRetries). Sequential strategies are collision-proof but require careful counter management. Use collision_strategy: 'fail' for critical paths and monitor retry logs.
Sequential Gaps If references are deleted, sequential counters may leave gaps (e.g., INV-0001 deleted → INV-0002 skips INV-0001). Document this behavior in the system design. Use reset_frequency (e.g., yearly) to mitigate gaps.
Multi-Tenancy Overhead Tenant-aware uniqueness adds complexity to queries (e.g., WHERE company_id = ? AND reference = ?). Ensure proper database indexing on (tenant_column, reference). Benchmark query performance under high concurrency.
Template Complexity Custom templates (e.g., {PREFIX}{YEAR}{SEQ}) may introduce parsing errors or edge cases (e.g., leap years, timezone handling). Test templates thoroughly with edge cases. Use dry-run in Artisan commands to validate formats before deployment.
Migration Downtime Adding the reference column to large tables may cause locks or slow migrations. Run migrations during low-traffic periods. Use batch operations (--batch=500) for backfilling references.
Vendor Lock-in Heavy reliance on package-specific traits/methods (e.g., generateReference(), validateReference()) may complicate future extraction. Abstract critical logic behind interfaces (e.g., ReferenceGenerator) to ease future decoupling.

Key Questions for TPM

  1. Business Requirements:

    • Are references user-facing (e.g., invoices) or internal-only (e.g., audit logs)? This impacts validation/format strictness.
    • Are there compliance/legal requirements for reference formats (e.g., tax invoices must include year/month)?
    • What is the expected scale (e.g., 1M references/year)? This affects sequential counter design and database indexing.
  2. Technical Constraints:

    • Does the application use multi-tenancy? If so, how are tenants isolated (database-per-tenant, shared DB with tenant_id)?
    • Are there existing reference systems that must integrate with this package (e.g., legacy APIs, third-party services)?
    • What is the deployment frequency? Frequent deployments may require careful handling of schema/migrations.
  3. Operational Considerations:

    • Who will own reference generation logic (e.g., developers, business analysts)? This affects configuration complexity.
    • Are there audit requirements for reference changes (e.g., tracking who regenerated a reference)?
    • What is the rollback plan if reference generation fails in production?
  4. Performance:

    • What is the expected read/write ratio for reference queries? High reads may benefit from caching.
    • Are there concurrency hotspots (e.g., 1000+ orders/minute)? Sequential strategies may need sharding (e.g., per-tenant counters).
  5. Future-Proofing:

    • Should the package support custom reference generators (e.g., integrate with external ID providers)?
    • Are there plans to expose references via APIs? If so, should they be masked (e.g., ORD-****-1234)?

Integration Approach

Stack Fit

  • Laravel Versions: Explicitly supports Laravel 10–13, with backward compatibility for v9 (via v1.x). Ideal for greenfield projects or gradual upgrades.
  • Database Compatibility: Works with MySQL, PostgreSQL, SQLite, and SQL Server (standard Laravel-supported databases). No vendor-specific features.
  • Ecosystem Synergy:
    • Eloquent Models: Native integration with Laravel’s ORM (e.g., findByReference() scopes).
    • Artisan Commands: CLI tools for bulk operations (e.g., referenceable:generate) align with Laravel’s dev workflow.
    • Testing: Built-in test suite and Laravel’s testing helpers (e.g., create(), factory()) simplify validation.

Migration Path

Phase Actions Tools/Commands
Assessment Audit existing models for reference needs. Identify high-priority models (e.g., Order, Invoice). Manual review + php artisan make:model analysis.
Pilot Implement in one model (e.g., Order) with default random strategy. Test generation, validation, and collision handling. composer require eg-mohamed/referenceable, php artisan referenceable:install, model trait addition.
Configuration Define global defaults (e.g., prefix: "ORD", separator: "-") and model-specific overrides (e.g., Invoice uses sequential with yearly reset). config/referenceable.php + model-level config.
Backfill Generate references for existing records using batch operations. php artisan referenceable:generate App\Models\Order --batch=500.
Validation Run dry-run validations to catch format/Uniqueness issues. php artisan referenceable:validate App\Models\Order --dry-run.
Monitoring Set up logging for reference generation failures (e.g., collision retries). Laravel’s Log facade + referenceMaxRetries tuning.
Rollout Gradually enable for other models. Replace auto-increment IDs with references in user-facing systems (e.g., emails, PDFs). Feature flags for reference visibility.
Optimization Add database indexes, tune cache TTL, and monitor performance under load. EXPLAIN queries, Laravel Debugbar, Blackfire.

Compatibility

  • Existing Code:
    • Breaking Changes: None for v2.x. v1.x users must update trait namespaces and config.
    • Deprecations: None in v2.x. Backward-compatible with v1.x configurations.
  • Third-Party Packages:
    • Potential Conflicts: None identified. The package uses Laravel’s standard service container and does not override core classes.
    • Dependencies: Only requires Laravel core and PHP 8.1+. No heavy dependencies (e.g., no Guzzle, Symfony components).
  • Custom Logic:
    • Extensibility: Supports custom generation strategies via service providers. Example:
      // app/Providers/ReferenceableServiceProvider.php
      public function register()
      {
          $this->app->extend('referenceable.strategy', function ($app) {
              return new CustomStrategy();
          });
      }
      

Sequencing

  1. Pre-Installation:
    • Review database schema for existing reference-like columns (e.g., `
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