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

Virtualcolumn Laravel Package

stancl/virtualcolumn

stancl/virtualcolumn adds database-like “virtual columns” to Eloquent models, letting you define computed attributes that can be queried, sorted, and indexed as if they were real fields. Useful for JSON data, derived values, and cleaner, faster queries.

View on GitHub
Deep Wiki
Context7

Getting Started

Begin by installing the package via Composer: composer require stancl/virtualcolumn. Next, include the HasVirtualColumns trait in any Eloquent model where you want to define computed attributes. Define virtual columns using the virtualColumns() method—return an array mapping keys (attribute names) to closures that accept the model instance and return computed values. The simplest use case is exposing a full name from first/last name fields on a User model, e.g., 'full_name' => fn($user) => $user->first_name . ' ' . $user->last_name.

Implementation Patterns

  • Encapsulate business logic: Place derived field logic inside the model via virtualColumns() to avoid repeating code in resources or controllers.
  • Dynamic computation: Use closures that accept dependencies (e.g., related models, config, or environment) to enable context-aware logic.
  • Complement accessor patterns: Use virtual columns instead of traditional accessors when the logic is lightweight, stateless, and needed across multiple contexts (e.g., API resources, queries).
  • Batched access: Leverage toArray() and toJson() to automatically serialize virtual columns alongside real attributes—ideal for API responses.
  • Reusability across models: Define a base trait or abstract model with shared virtual columns to reuse logic across multiple Eloquent classes.

Gotchas and Tips

  • Avoid expensive computations: Since virtual columns are calculated in PHP for every model instance, do not run N+1 queries or heavy processing without caching (e.g., via withCount() or eager-loaded relations inside the closure).
  • Eager loading support: Virtual columns do not participate in query scopes or select()—they’re resolved at runtime. Use them for presentation or final-level logic, not for SQL WHERE/ORDER BY.
  • Testing: Unit-test virtual column logic by instantiating models with mock data or factories and asserting getAttribute('virtual_attr') behavior.
  • Serialization quirks: Virtual columns appear in toArray()/toJson() by default, but use $appends = ['virtual_attr'] if you want them always included—even in non-resource contexts.
  • Extensibility: Extend VirtualColumn class or use a custom resolver if you need more advanced behavior (e.g., caching, lazy-loading, or type-casting).
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
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
twbs/bootstrap4