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

Laravel Computed Attributes Laravel Package

korridor/laravel-computed-attributes

Adds “computed attributes” to Laravel models, letting you define dynamic/derived properties that behave like normal attributes (including access/casting/serialization) without storing them in the database. Useful for clean model APIs and reusable calculations.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Dynamic Data Flexibility: Enables real-time computed fields (e.g., pricing tiers, derived metrics, or normalized data) without manual database updates, reducing backend complexity. Supports Laravel 13 with backward compatibility for older versions, ensuring long-term viability.
  • Roadmap Acceleration: Shortens development cycles for features requiring derived attributes (e.g., "total_order_value" or "customer_lifetime_value") by eliminating manual sync logic. New: Enhanced CI/CD with static code analysis (PHPStan) improves code quality and reduces runtime bugs, aligning with modern DevOps practices.
  • Build vs. Buy: Avoids reinventing computed attribute logic (e.g., caching, recalculation triggers) while maintaining Laravel’s Eloquent ecosystem. New: Updated dependencies (e.g., GitHub Actions, PHPStan) ensure long-term maintainability and security.
  • Use Cases:
    • E-commerce: Dynamic pricing (e.g., discounts, taxes, shipping costs).
    • Analytics: Pre-computed KPIs (e.g., "user_engagement_score," "customer_lifetime_value").
    • Data Migration: Backfill computed fields without downtime or complex scripts.
    • Modern Laravel Apps: Ideal for teams adopting Laravel 10–13, as this package guarantees compatibility and leverages the latest Laravel features (e.g., strict types, PHP 8.3).
    • APIs: Auto-append computed attributes to JSON responses (via $appends) for consistency.
    • Reporting: Store derived metrics (e.g., "monthly_recurring_revenue") in the DB for faster queries.

When to Consider This Package

  • Adopt if:
    • Your application requires persistent computed fields tied to model data (e.g., financial calculations, aggregations, or normalized data).
    • You need to reduce query complexity by caching derived values in the database (e.g., avoiding redundant joins or calculations).
    • Your team uses Laravel 10–13 and wants a declarative solution over raw SQL triggers or manual sync logic.
    • You prioritize code quality and want static analysis (PHPStan) and modern CI/CD (GitHub Actions) in your pipeline.
    • You’re building features that require real-time derived data (e.g., dashboards, admin panels) without sacrificing performance.
  • Look elsewhere if:
    • Computed fields are purely transient (use standard Eloquent accessors/mutators).
    • Your data model requires complex event-driven recalculations (e.g., event sourcing, CQRS, or outbox patterns).
    • You’re constrained by strict database schemas (e.g., read-only replicas, legacy systems without write access).
    • You’re not using Laravel or are on an unsupported version (though backward compatibility exists for older Laravel releases).
    • Your use case involves high-frequency writes where computed attributes would introduce significant overhead (consider caching layers like Redis instead).

How to Pitch It (Stakeholders)

For Executives/Stakeholders: "This package lets us store derived data (like dynamic pricing, user metrics, or order totals) directly in the database—automatically recalculating and persisting values when underlying data changes. For example, instead of recalculating a customer’s lifetime value every time we query their data, we compute it once and store it, cutting database load and speeding up responses. Now fully compatible with Laravel 13, it includes modern tooling like PHPStan to catch bugs early, ensuring long-term reliability. It’s a ‘set it and forget it’ solution for complex derived fields, reducing backend complexity and accelerating feature delivery."

For Engineering/Technical Leads: *"This package extends Eloquent to auto-save computed attributes (e.g., order_total = subtotal + tax) to the database, eliminating the need for manual sync logic or triggers. Key benefits:

  • Laravel 13 support: Officially compatible with the latest Laravel LTS.
  • Static analysis: PHPStan integration in CI improves code quality.
  • Performance: Reduces redundant calculations by caching derived values in the DB.
  • Flexibility: Works with dynamic computations (e.g., conditional logic, API calls) and integrates with Eloquent events.
  • Maintainability: MIT-licensed, actively maintained, and aligns with modern Laravel practices (strict types, GitHub Actions).

Tradeoffs: Adds a column per computed field, but the performance gain from avoiding recalculations outweighs this. Ideal for e-commerce, analytics, or any app with derived metrics. Updated dependencies ensure long-term reliability."*

For Developers: *"Think of this as Eloquent accessors on steroids—your computed fields are now persisted to the DB and stay in sync. No more writing custom observers or triggers for derived data. Example:

protected $computedAttributes = [
    'formatted_email' => fn() => strtolower($this->email),
    'discounted_price' => fn() => $this->price * (1 - $this->discount),
];
  • Automatic persistence: Values save when the model updates.
  • Caching control: Disable caching for volatile data or set TTLs.
  • Validation: Built-in artisan computed-attributes:validate to check consistency.
  • Laravel 13 ready: Uses strict types and modern PHP features.
  • Extensible: Hook into computation events or override storage logic.

Perfect for features like dynamic pricing, user metrics, or any derived field you’d otherwise recalculate on every query."*

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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata