Search by

flynn314 / data-field

Flynn314

Data field

Package info

github.com/flynn314/data-field

pkg:composer/flynn314/data-field

Statistics

Installs: 1 226

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

3.3.0 2026-11-22 13:46 UTC

This package is auto-updated.

Last update: 2026-11-22 13:54:26 UTC


README

PHP 8.4+ Laravel 11+ License: Apache 2.0

Store flexible, schema-free metadata in a data JSON column on an Eloquent model. Use ordinary columns for your core schema and data for optional settings, flags, and other values that vary between records.

Installation

Requires PHP 8.4+ and Laravel 11+.

composer require flynn314/data-field

In a migration, add a data column to your model's table:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Schema::table('projects', function (Blueprint $table): void {
    $table->json('data')->nullable();
});

Run php artisan migrate. Add DataFieldSetGet to the model; no service provider or config file is needed.

Quick start

use Flynn314\DataField\DataFieldSetGet;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use DataFieldSetGet;
}
$project = new Project();

$project->setData('theme', 'dark');
$project->setData('notifications.email', true);
$project->setData('tags.', 'internal'); // Append to a list.
$project->setData('tags.', 'priority');
$project->save();

$project->getData('theme', 'light');        // 'dark'
$project->getData('notifications.email');   // true
$project->getData('tags');                  // ['internal', 'priority']
$project->getData('missing', 'fallback');   // 'fallback'

$project->unsetData('notifications.email');
$project->setData('theme', null); // Also removes the key.
$project->save();

getData() and setData() work with JSON-compatible values. They support parent.child for one level of nesting; a trailing dot in setData('key.', $value) appends to a list. Changes stay in memory until you call save().

Casts

Declare casts for top-level JSON keys in the model's casts() method, just as you do for regular Eloquent attributes. The public DataCast constants are strings, so no ->value is needed.

use Flynn314\DataField\DataCast;
use Flynn314\DataField\DataFieldSetGet;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use DataFieldSetGet;

    protected function casts(): array
    {
        return [
            'labels' => DataCast::ARRAY,
            'enabled' => DataCast::BOOLEAN,
            'published_at' => DataCast::DATETIME,
            'priority' => DataCast::INTEGER,
        ];
    }
}
Constant String Behavior inside data
DataCast::ARRAY array Accepts an array without converting it.
DataCast::BOOLEAN boolean Converts the value to a boolean.
DataCast::DATETIME datetime Stores DateTimeInterface values as Y-m-d H:i:s and reads them as DateTime.
DataCast::HASHED hashed Hashes the value on write and returns the stored hash on read.
DataCast::INTEGER integer Converts the value to an integer. int is also accepted.

Dot-path access does not apply casts. These are the casts supported inside data; ordinary model columns still use Eloquent's own cast handling. The trait defaults the data container to AsArrayObject. Do not cast the container itself to array or json: setData() and unsetData() require AsArrayObject or a subclass.

Automatic casts for the primary key and soft-delete column do not carry over to JSON keys. created_at, updated_at, and deleted_at remain ordinary Eloquent columns; $project->getData('deleted_at') reads a separate JSON value.

Optional: expose JSON keys as model attributes

Add #[DataFields] to route selected keys through normal Eloquent property access:

use Flynn314\DataField\DataFieldSetGet;
use Flynn314\DataField\DataFields;
use Illuminate\Database\Eloquent\Model;

#[DataFields(['theme'])]
class Project extends Model
{
    use DataFieldSetGet;
}
$project->theme = 'dark'; // Stored in data, not in a separate column.
$project->theme;          // 'dark'
$project->toArray();      // Includes 'theme' as a top-level key when visible.

A subclass inherits its nearest parent's #[DataFields] declaration unless it declares its own. Avoid listing data, real columns, or relationships: virtual fields take precedence over normal attribute access.

Serialization: Eloquent's $hidden and $visible control the top-level virtual fields, but they do not filter individual keys inside the data container. If data contains sensitive values, hide the entire container and expose only safe fields, for example through an API Resource.

Development

Run the test suite from the package directory:

composer install
vendor/bin/phpunit

License

Apache License 2.0.