PHPackages                             moirei/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. [Database &amp; ORM](/categories/database)
4. /
5. moirei/laravel-model-data

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

moirei/laravel-model-data
=========================

Easily handle data fields in your Eloquent models

1.2.0(4y ago)23741MITPHPPHP ^7.2|^8.0CI failing

Since Dec 13Pushed 4y ago1 watchersCompare

[ Source](https://github.com/augustusnaz/laravel-model-data)[ Packagist](https://packagist.org/packages/moirei/laravel-model-data)[ Docs](https://github.com/augustusnaz/laravel-model-data)[ RSS](/packages/moirei-laravel-model-data/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (7)Used By (0)

laravel-model-data
==================

[](#laravel-model-data)

This package allows you to access attribute fields in your model or use a **data** field without any changes to your migrations.

### Features

[](#features)

- Use extra data on eloquent model without defining a new column
- Easily interact with multiple custom fields on your model as data fields
- `collect` any data and save into any model with the `HasData` trait

Installation &amp; Setup
------------------------

[](#installation--setup)

You can install the package via composer;

```
composer require moirei/laravel-model-data
```

Add the `MOIREI\ModelData\HasData` trait to your model;

```
use MOIREI\ModelData\HasData;

class YourModel extends Model
{
    use HasData;

    ...
}
```

#### Using the package's model

[](#using-the-packages-model)

*Optional if using a predefined attribute/column in your models.*

Publish the migration with;

```
php artisan vendor:publish --tag="model-data-migrations"
```

Run the migrate command to create the necessary table;

```
php artisan migrate
```

### Mode 1: Persist data outside your model

[](#mode-1-persist-data-outside-your-model)

This is the default mode if the `$model_data` property is falsy.

### Mode 2: Persist data in your model

[](#mode-2-persist-data-in-your-model)

Define the data column (s) in your migration;

```
Schema::create('products', function (Blueprint $table) {
    $table->modelData('data');
    // OR
    $table->json('data')->nullable();
    // OR
    $table->text('data')->nullable();
});
```

Then define a `model_data` variable in your model. This is the name of the storage column(s).

```
...

/**
  * ModelData: use model's column
  *
  * @var string|array|false
  */
protected $model_data = 'data';

class YourModel extends Model
{
    use HasData;

    ...
}
```

Usage
-----

[](#usage)

### Accessing the data

[](#accessing-the-data)

The below uses *`data`* if in *mode 1* or the value of `$model_data` is `"data"`;

Basic access:

```
$model->data->name = 'value';
$model->data->name; // Returns 'value'
```

Access as arrays:

```
$model->data['name'] = 'value';
$model->data['name']; // Returns 'value'
```

Calling as function.

```
$model->data('name', 'value');
$model->data('name'); // Returns 'value'

$model->data('moirei.tech', 'awesome');
$model->data('moirei.tech'); // Returns 'awesome'
```

All existing data can be overridden by assigning an array;

```
// All existing data will be replaced
$model->data = ['name' => 'value'];
$model->data->all(); // Returns ['name' => 'value']
```

With `get` and `set`;

```
$model->data = [
   'moirei' => ['tech' => 'awesome'],
   'mg001' => ['resource' => 'white'],
];
// or
$model->data->set([
   'moirei' => ['tech' => 'awesome'],
   'mg001' => ['resource' => 'white'],
]);
// or
$model = $model->data([
   'moirei' => ['tech' => 'awesome'],
   'mg001' => ['resource' => 'white'],
]);

$model->data->set('mg001.name', 'Wireless Power Bank');
$model->data->get('mg001.name'); // Returns 'Wireless Power Bank'
```

`get` with default:

```
$model->data->get('undefined-attribute', 'default'); // Returns 'default'
```

### Multiple/custom data fields (mode 2)

[](#multiplecustom-data-fields-mode-2)

The above example uses `data` field which is the default for external mode. To allow multiple access with custom names,

```
...

/**
  * ModelData: use model's column
  *
  * @var string|array|false
  */
// protected $model_data = 'settings';
protected $model_data = [
    'settings', 'meta',
];

...
```

Access with

```
// set values
$model->settings->set([]);
$model->meta->set([]);

// get values
dump($model->settings());
dump($model->meta());
```

### Persisting data

[](#persisting-data)

```
$model->save();
// or
$model->data->save();
```

Modify and save into a different model with

```
$model->data->filter()->save($model_2, $key = 'data');
```

### Collections

[](#collections)

This packages supplies Collections functions `pinch` and `save` globally.

This means you can collect and save any data into any model that has the `HasData` trait;

```
$data = collect([
    'first_name' => 'James',
    'last_name' => 'Franco'
])->save($model, $key = 'data');
```

The `pinch` function simply allows you to access a collection's underlying array using the dot notation.

The `key` option defaults to `data`.

Credits
-------

[](#credits)

- [Augustus Okoye](https://github.com/augustusnaz)

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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

Every ~113 days

Recently: every ~141 days

Total

6

Last Release

1778d ago

PHP version history (2 changes)1.0.0PHP ^7.2

1.1.1PHP ^7.2|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7ac6a15a7b2f9055a98e4419e0d2820f72db3567706837ec388baee535d3e3e7?d=identicon)[moirei](/maintainers/moirei)

---

Top Contributors

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

---

Tags

laraveldatamodeleloquentmetamoireiextra datamodel data

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[kodeine/laravel-meta

Fluent Meta Data for Eloquent Models, as if it is a property on your model.

426756.0k9](/packages/kodeine-laravel-meta)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)

PHPackages © 2026

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