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.
GENERATED ALWAYS AS), with the flexibility to handle complex PHP logic (e.g., API calls, conditional logic).updating, saved, etc.), enabling fine-grained control over recomputation triggers.cache flag or recompute() calls.artisan vendor:publish).use HasComputedAttributes).$computedAttributes.StoresComputedAttributesInJson).computed-attributes:validate) for validation, easing adoption in CI/CD pipelines.artisan computed-attributes:validate).save(), adding latency. Mitigate with:
skip_on_create flag for non-critical attributes.invalidateComputedAttribute() calls.ComputedAttributes::computing()).isComputedAttributeCached() checks.cache: false for dependent attributes.cache: false or queue jobs?user_reputation_score fetched from a third-party service.save() be acceptable? If not, should we explore:
BEFORE UPDATE).computed-attributes:validate command or custom assertions?order_total matches subtotal + tax after updates.null value with a fallback logic?@computed PHPDoc tag to models.GENERATED) be more performant?computed-attributes:validate).updating, saved, etc.StoresComputedAttributesInJson).isDirty() checks for debugging.order_total = subtotal + tax logic in controllers with a computed attribute.cache: false initially to validate logic before enabling persistence.artisan computed-attributes:validate or a custom seeder.OrderController@calculateTotal() if order_total is now a computed attribute.recomputeComputedAttribute() to force updates.$fillable but require explicit handling to avoid infinite loops.$appends or explicitly added.appends.user.order.total).composer require korridor/laravel-computed-attributes
php artisan vendor:publish --provider="Korridor\ComputedAttributes\ComputedAttributesServiceProvider" --tag="config"
config/computed-attributes.php (e.g., default TTL, storage engine).default_cache_ttl to 3600 for frequently accessed attributes.use HasComputedAttributes to target models.$computedAttributes array (start with 1–2 attributes per model).artisan computed-attributes:validate to check existing data.How can I help you explore Laravel packages today?