simplecms/dynamic_unit

动态单元组件,可以丰富你的Model,让其具备更多的单元属性,无需对数据表进行多次修改调整, 仅需要简单调用即可让单一Model增加更多可控属性

Maintainers

Package info

github.com/hackout/simplecms-dynamic_unit

pkg:composer/simplecms/dynamic_unit

Transparency log

Statistics

Installs: 11

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.4 2026-08-30 17:57 UTC

This package is auto-updated.

Last update: 2026-08-30 18:17:28 UTC


README

📦 A flexible dynamic attribute package for SKU, dictionary-like metadata, and polymorphic model properties.

English | 简体中文

Latest Stable Version Latest Unstable Version Total Downloads License

Features

  • Dynamic unit definition and management
  • Dynamic attribute items with optional file/media attachment
  • Polymorphic model relations for custom attribute storage
  • Query by attribute code
  • Compatible with Laravel 11 and 12
  • Facade and SimpleCMS service integration

Requirements

  • PHP ^8.2
  • MySQL 8.0+
  • Laravel ^11.0 or ^12.0
  • Optional: simplecms/framework ^1.0

Installation

composer require simplecms/dynamic_unit
php artisan migrate

If package discovery is not enabled, register the provider manually:

// config/app.php
SimpleCMS\DynamicUnit\DynamicUnitServiceProvider::class,

Quick Start

1. Enable dynamic attributes on your model

use Illuminate\Database\Eloquent\Model;
use SimpleCMS\DynamicUnit\Traits\DynamicAttributeTrait;

class Product extends Model
{
    use DynamicAttributeTrait;
}

This adds the customsAttributes morphMany relationship.

2. Filter by attribute codes

$array = ['red', 'blue'];

$products = Product::query()
    ->withAttributeCodes($array)
    ->get();

3. Create a dynamic unit

use SimpleCMS\DynamicUnit\Facades\DynamicUnit;

DynamicUnit::createUnit([
    'name' => 'Color',
    'code' => 'color',
    'items' => [
        ['name' => 'Red', 'code' => 'red'],
        ['name' => 'Blue', 'code' => 'blue'],
    ],
]);

4. Query a dynamic unit

$all = DynamicUnit::getAll();
$unit = DynamicUnit::findByCode('color');
$items = DynamicUnit::findListByCode('color');
$attr = DynamicUnit::findAttributeByCode('color', 'red');

5. Update and delete

DynamicUnit::updateUnit(1, [
    'name' => 'Color',
    'code' => 'color',
    'items' => [
        ['id' => 10, 'name' => 'Red', 'code' => 'red'],
        ['name' => 'Green', 'code' => 'green'],
    ],
]);

DynamicUnit::deleteAttribute(10);
DynamicUnit::deleteUnit(1);

Facade API

use SimpleCMS\DynamicUnit\Facades\DynamicUnit;

DynamicUnit::getAll();
DynamicUnit::findByCode(string $code);
DynamicUnit::findListByCode(string $code);
DynamicUnit::findAttributeByCode(string $code, string $attributeCode);

DynamicUnit::createUnit(array $data);
DynamicUnit::updateUnit(int $id, array $data);
DynamicUnit::deleteUnit(int $id);

DynamicUnit::createAttribute(array $data);
DynamicUnit::updateAttribute(int $id, array $data);
DynamicUnit::deleteAttribute(int $id);

SimpleCMS Extension

If you use simplecms/framework, you can call the service extension directly:

use SimpleService;

$service = app(SimpleService::class);
$service->queryAttribute(['red', 'blue']);

Fixes included in this version

This package version includes fixes for common runtime issues such as:

  • incorrect belongsTo mapping in DynamicAttribute and CustomAttribute
  • null-safe handling when a code target is missing
  • incorrect unit updates that failed to sync items
  • Laravel 11/12 dependency compatibility

Migration notes

The package creates the following tables:

  • dynamic_units
  • dynamic_attributes
  • customs_attributes

Migration files are located in:

database/migrations/

If you need a clean reset:

php artisan migrate:fresh

Common issues

1. Empty query results

Check that:

  • the model uses DynamicAttributeTrait
  • dynamic_attribute_id matches dynamic_attributes.id
  • the code values were saved successfully

2. Updated unit does not reflect child attributes

Ensure the items array contains:

  • new entries without id
  • existing entries with id
  • removed items should not be present in the final array

3. Package discovery not working

Try:

php artisan optimize:clear

License

MIT