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

Laravel package that adds computed attributes to eloquent models. A computed attribute is an accessor were the computed value is saved in the database.

Frequently asked questions about Laravel Computed Attributes
How do I define a computed attribute in a Laravel Eloquent model using this package?
Use the `$computedAttributes` property in your model and apply the `HasComputedAttributes` trait. For example, add `protected $computedAttributes = ['full_name'];` and define an accessor for `getFullNameAttribute()`. The package will automatically persist the computed value to the database column you specify.
Does this package work with Laravel 13, and what’s the minimum supported version?
Yes, the package officially supports Laravel 10 through 13. Version 3.3.0+ explicitly targets Laravel 13, while older versions (e.g., 2.x) support Laravel 9–10. Always check the package’s `composer.json` for the latest compatibility matrix.
Will computed attributes break JSON serialization if I forget to include them in `$appends`?
Yes, computed attributes won’t appear in JSON responses unless you explicitly add them to the `$appends` array in your model. This is a common pitfall—document this requirement in your team’s coding standards or use a base model trait to enforce it.
Can I use computed attributes for expensive calculations, like API calls or heavy processing?
While possible, expensive computations during `save()` or `update()` can slow down bulk operations. Mitigate this by disabling caching (`cache: false`) for volatile attributes or offloading the work to Laravel queues. For critical performance cases, consider database-level computed columns instead.
How do I handle schema migrations for computed attributes? Do I need to add a new column for each one?
Yes, you must add a database column for each computed attribute to store its value. Use Laravel migrations to create the column, and the package will handle the rest. For dynamic schemas (e.g., JSON columns), use `StoresComputedAttributesInJson` to avoid rigid column constraints.
What happens if two computed attributes depend on each other (e.g., A depends on B, which depends on A)?
This creates a circular dependency that can cause infinite loops or incorrect values. The package doesn’t prevent this by default, but you can mitigate it by disabling caching (`cache: false`) for the conflicting attributes or restructuring your logic to avoid mutual dependencies.
Is this package a good alternative to PostgreSQL’s `GENERATED ALWAYS AS` or MySQL 8+ computed columns?
This package is ideal for PHP-side logic that doesn’t justify database-level computed columns, such as complex business rules or external API integrations. For pure SQL-based calculations, database computed columns are more efficient, but this package offers flexibility for hybrid scenarios.
How do I test computed attributes in my CI pipeline? Are there built-in test helpers?
The package includes unit tests, but you’ll need to write custom assertions for your computed attributes. Use Laravel’s `assertEquals()` or create helper methods like `assertComputedAttributeEquals($model, 'attribute', $expected)`. Mock external dependencies (e.g., APIs) in your tests to ensure consistency.
Can I use computed attributes with Laravel’s `update()` or `increment()` methods without performance issues?
Performance may degrade during bulk operations if computed attributes trigger expensive logic. For large-scale updates, consider disabling caching (`cache: false`) or using database-level computed columns. Test with your expected workload to identify bottlenecks.
What’s the maintenance status of this package, and does it support Laravel 14 when it releases?
The package is actively maintained with GitHub Actions CI/CD and PHPStan checks. While Laravel 14 support isn’t guaranteed yet, the maintainers have shown responsiveness to version updates. Monitor the GitHub repo or check the changelog for announcements on Laravel 14 compatibility.
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