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

This package is auto-updated.

Last update: 2025-04-29 19:31:02 UTC


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

  1. Add the detail column to your migration:
Schema::create('your_table', function (Blueprint $table) {
    $table->id();
    $table->json('detail');
    $table->timestamps();
});
  1. 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.