PHPackages                             jsefton/laravel-virtual-fields - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jsefton/laravel-virtual-fields

ActiveLibrary[Database &amp; ORM](/categories/database)

jsefton/laravel-virtual-fields
==============================

Allow eloquent models to store and retreive data against anything without needing a specific database column

1.0.0(5y ago)11.4k1MITPHP

Since May 3Pushed 5y ago2 watchersCompare

[ Source](https://github.com/jsefton/laravel-virtual-fields)[ Packagist](https://packagist.org/packages/jsefton/laravel-virtual-fields)[ Docs](https://endor.digital)[ RSS](/packages/jsefton-laravel-virtual-fields/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (1)

Laravel Virtual Fields
======================

[](#laravel-virtual-fields)

Allow eloquent models to store and retreive data against anything without needing a specific database column.

Virtual Fields hook into eloquent attribute fetching to allow it to use a specific field to map data but still support all the core features of eloquent and a hybrid of virtual and physical fields in a database table.

Installation
============

[](#installation)

```
composer require jsefton/laravel-virtual-fields
```

Usage &amp; Requirements
========================

[](#usage--requirements)

Virtual Fields is a trait that you can use in any eloquent model. e.g.

```
use Illuminate\Database\Eloquent\Model;
use JSefton\VirtualFields\VirtualFields;

class Post extends Model
{
    use VirtualFields;
}
```

For virtual fields to work the database table for the desired model must have a field called `data`. You should add this through a migration for each model / table you wish to use Virtual Fields.

```
php artisan make:migration add_virtual_field_data_table_to_{table} --table={table}
```

Once you have created a migration you will want something similar to the below to add the new field and handle removing in the down method.

```
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('{table}', function (Blueprint $table) {
        $table->longText('data')->nullable();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('{table}', function (Blueprint $table) {
        $table->dropColumn(['data']);
    });
}
```

The reason for `data` field to be a `longText` is to support long term usage of Virtual Fields. If you keep adding new data to store via Virtual Fields it will end up with a large JSON object within this column. If you know it is only going to be used for a small amount of data then please make your migration use a suitable data type.

Example
-------

[](#example)

Once setup you can simply use normal attribute getting &amp; setting against a model. This will automatically be handled when being saved into the database againt the model and converted back to actual attributes on query.

In this example we have a Post and a virtual field of `sub_title`. This field does not exist as a physical field in the database schema.

```
$post = Post::find(1);

// Set a virtual field of sub_title
$post->sub_title = 'Example post sub title';
$post->save();
```

The `sub_title` will be saved in the `data` field mapped as an array with `sub_title` key and `Example post sub title` as a value.

When you want to get the value back out to use you simply use it like any normal eloquent attribute:

```
echo $post->sub_title;
// outputs: Example post sub title
```

Please note currently for Laravel 7+ until tested and verified in lower versions.

### TODO

[](#todo)

- Add in support for querying easily against Virtual Fields without JSON query requirement.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1842d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/41b413127c7499a63bcb5c5ae903bd8961ba29300b6ccbda873e0c4bf06d0a02?d=identicon)[jsefton](/maintainers/jsefton)

---

Top Contributors

[![jsefton](https://avatars.githubusercontent.com/u/4114741?v=4)](https://github.com/jsefton "jsefton (4 commits)")

---

Tags

laravelschemadataeloquentfieldsvirtual

### Embed Badge

![Health badge](/badges/jsefton-laravel-virtual-fields/health.svg)

```
[![Health](https://phpackages.com/badges/jsefton-laravel-virtual-fields/health.svg)](https://phpackages.com/packages/jsefton-laravel-virtual-fields)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11120.2M21](/packages/anourvalar-eloquent-serialize)[devio/eavquent

EAV modeling package for Eloquent and Laravel.

6410.4k](/packages/devio-eavquent)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
