bedoya/has-data

Trait to manage dynamic JSON fields in Laravel Eloquent models

v1.0.0 2025-06-21 03:52 UTC

This package is auto-updated.

Last update: 2025-06-21 03:57:09 UTC


README

A Laravel trait to manage dynamic JSON fields (usually named data) in Eloquent models, with elegant access, flexible customization, and auto-save support.

📦 Installation

You can install the package via Composer:

composer require bedoya/has-data

Optionally, publish the configuration file:

php artisan vendor:publish --tag=has-data-config

🚀 Usage

Add the trait to any Eloquent model that has a JSON column (default: data):

use Bedoya\HasData\Traits\HasData;

class Raffle extends Model
{
    use HasData;

    protected $casts = [
        'data' => 'array',
    ];
}

✅ Access data using dot notation

$raffle->getData('grid.rows');        // returns 25
$raffle->getData('grid');             // returns [25, 40]
$raffle->setData('grid.cols', 40);    // sets the value and optionally saves
$raffle->hasData('grid.rows');        // returns true

⚙️ Configuration

Default config (config/has-data.php)

return [
    'database' => [
        'column-name' => 'data',    // default JSON column name
        'nullable'    => true,
        'casts'       => 'array',
    ],
    'auto_save' => true,            // auto-save after setData()
];

🧠 Per-model configuration

You can override the JSON column or auto-save behavior per model.

Custom column per model

class Client extends Model
{
    use HasData;

    protected string $dataColumn = 'metadata';
    protected $casts = [
        'metadata' => 'array',
    ];
}

Disable auto-save per model

class Client extends Model
{
    use HasData;

    public bool $hasDataAutoSave = false;
}

If hasDataAutoSave is not set, it falls back to the global config.

🧪 Testing

This package includes full test coverage using PestPHP. To run the tests:

composer test

🧱 Requirements

  • PHP 8.1+
  • Laravel 10 or 11
  • JSON-compatible database column (casted to array)

📄 License

This package is open-sourced software licensed under the MIT license.

✍️ Author

Andrés Bedoya