service-to / uses-detail
A Laravel trait for handling dynamic model attributes using a JSON detail column
v1.0.1
2025-04-29 19:29 UTC
Requires
- php: ^8.0
- illuminate/database: ^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^7.0|^8.0
- phpunit/phpunit: ^9.5
README
A Laravel trait that enables dynamic model attributes using a JSON detail column.
Installation
You can install the package via composer:
composer require service-to/uses-detail
Usage
- Add the
detail
column to your migration:
Schema::create('your_table', function (Blueprint $table) { $table->id(); $table->json('detail'); $table->timestamps(); });
- Use the trait in your model:
use ServiceTo\UsesDetail; class YourModel extends Model { use UsesDetail; }
Features
- Automatically stores non-database columns in a JSON detail column
- Provides UUID generation for models
- Enables searching within the detail column using the
detail
scope - Supports merging objects into the model
- Handles JSON encoding/decoding automatically
Methods
scopeDetail($query, $column, $operator = null, $value = null, $boolean = 'and')
Search within the detail column:
YourModel::detail('key', '=', 'value')->get();
merge($obj)
Merge an object or JSON string into the model:
$model->merge($object); $model->merge('{"key": "value"}');
License
The MIT License (MIT). Please see License File for more information.