PHPackages                             centrex/laravel-model-data - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. centrex/laravel-model-data

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

centrex/laravel-model-data
==========================

Add virtual columns in any model of laravel

v1.1.0(1y ago)01.4k[4 PRs](https://github.com/centrex/laravel-model-data/pulls)MITPHPPHP ^8.2|^8.3|^8.4CI passing

Since Nov 19Pushed 1mo agoCompare

[ Source](https://github.com/centrex/laravel-model-data)[ Packagist](https://packagist.org/packages/centrex/laravel-model-data)[ Docs](https://github.com/centrex/laravel-model-data)[ RSS](/packages/centrex-laravel-model-data/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (14)Versions (10)Used By (0)

Add virtual columns to any Eloquent model
=========================================

[](#add-virtual-columns-to-any-eloquent-model)

[![Latest Version on Packagist](https://camo.githubusercontent.com/754f8b2105b34f59e55f5b9bf6531fdf4129393eac1d8244eaae74a25c896a7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63656e747265782f6c61726176656c2d6d6f64656c2d646174612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/centrex/laravel-model-data)[![GitHub Tests Action Status](https://camo.githubusercontent.com/aa7965dbfac9b630bb702f9765c92a39d91a93c20b476b295ae9acdabe7a6beb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63656e747265782f6c61726176656c2d6d6f64656c2d646174612f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/centrex/laravel-model-data/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/e5b6e57882a8cacb50b03dbf176793fd0cd8545b360e515c300e78aac18523d5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63656e747265782f6c61726176656c2d6d6f64656c2d646174612f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/centrex/laravel-model-data/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/c075657ba5e5862dd8921f9d18b3f8a18efc62d023751de42495b56389367907/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63656e747265782f6c61726176656c2d6d6f64656c2d646174613f7374796c653d666c61742d737175617265)](https://packagist.org/packages/centrex/laravel-model-data)

Serialize arbitrary attributes that don't have a dedicated column into a single JSON `data` column via a polymorphic `model_datas` table. Attributes are transparently encoded on save and decoded on retrieval — models behave as if the virtual columns exist natively.

Installation
------------

[](#installation)

```
composer require centrex/laravel-model-data
php artisan vendor:publish --tag="model-data-migrations"
php artisan migrate
```

Usage
-----

[](#usage)

### 1. Add the trait to your model

[](#1-add-the-trait-to-your-model)

```
use Centrex\ModelData\HasData;

class Product extends Model
{
    use HasData;
}
```

### 2. Set and get virtual attributes

[](#2-set-and-get-virtual-attributes)

```
$product = Product::create(['name' => 'Widget']);

// Set virtual attributes (stored in model_datas table as JSON)
$product->color  = 'red';
$product->weight = 1.5;
$product->save();

// Retrieve — fully transparent, works like regular attributes
$product = Product::find($product->id);
echo $product->color;   // 'red'
echo $product->weight;  // 1.5
```

### 3. Query virtual attributes

[](#3-query-virtual-attributes)

```
// getColumnForQuery() returns the correct SQL expression
$column = $product->getColumnForQuery('color'); // → 'data->color'

Product::whereRaw("{$column} = ?", ['red'])->get();
```

### 4. Customize the real columns

[](#4-customize-the-real-columns)

Override `getCustomColumns()` to list columns that exist as real database columns (not stored in the JSON blob):

```
class Product extends Model
{
    use HasData;

    public static function getCustomColumns(): array
    {
        return ['id', 'name', 'price', 'created_at', 'updated_at'];
    }
}
```

Any attribute not in this list is automatically routed through the `data` JSON column.

### 5. Data types

[](#5-data-types)

Supported cast types via `DataType` enum: `string`, `integer`, `float`, `boolean`, `array`, `json`.

```
protected $casts = [
    'is_featured' => 'boolean',
    'tags'        => 'array',
];
```

Casts work normally — the trait intercepts encode/decode at the Eloquent event level.

Testing
-------

[](#testing)

```
composer test        # full suite
composer test:unit   # pest only
composer test:types  # phpstan
composer lint        # pint
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [centrex](https://github.com/centrex)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance70

Regular maintenance activity

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 90.9% 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

Every ~111 days

Total

5

Last Release

464d ago

PHP version history (3 changes)v1.0.0PHP ^8.0|^8.1

v1.0.2PHP ^8.1|^8.2

v1.1.0PHP ^8.2|^8.3|^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/3b62fd49922fd1bf7d55b94cc82ebc9f359af7f343b246a7e3a2642779c0b4e7?d=identicon)[rochi88](/maintainers/rochi88)

---

Top Contributors

[![rochi88](https://avatars.githubusercontent.com/u/29769944?v=4)](https://github.com/rochi88 "rochi88 (30 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

laravelcentrexlaravel-model-data

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/centrex-laravel-model-data/health.svg)

```
[![Health](https://phpackages.com/badges/centrex-laravel-model-data/health.svg)](https://phpackages.com/packages/centrex-laravel-model-data)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[laracraft-tech/laravel-useful-additions

A collection of useful Laravel additions!

58109.4k](/packages/laracraft-tech-laravel-useful-additions)[glhd/special

1929.4k](/packages/glhd-special)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)

PHPackages © 2026

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