waad/laravel-model-metadata
Composer 安装命令:
composer require waad/laravel-model-metadata
包简介
A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.
README 文档
README
Laravel Model Metadata
Laravel Model Metadata is a package designed to manage metadata with JSON support for multiple data types. It allows you to easily attach, manage, and query metadata on your Laravel models using the HasManyMetadata or HasOneMetadata traits.
📚 Documentation
For detailed documentation, including usage examples and best practices, please refer to the Documentation.
✨ Requirements
- PHP 8.0 or higher
- Laravel framework 9.30.1 or higher
- JSON extension enabled
💼 Installation
-
Install the package using Composer:
composer require waad/laravel-model-metadata
-
(Optional) Publish the config file first:
php artisan vendor:publish --tag=metadata-config
-
(Recommended) Clear the configuration cache to ensure new config is loaded:
php artisan config:clear
-
Publish the migration files:
php artisan vendor:publish --tag=metadata-migrations
-
Run the migrations:
php artisan migrate
⚙️ Configuration
You can customize the metadata table name, model, and caching behavior by editing the published config file at config/model-metadata.php:
return [ 'table' => 'model_metadata', 'model' => Waad\Metadata\Models\Metadata::class, 'cache' => [ 'enabled' => env('MODEL_METADATA_CACHE_ENABLED', false), 'ttl' => env('MODEL_METADATA_CACHE_TTL', 3600), 'store' => env('MODEL_METADATA_CACHE_STORE', null), 'prefix' => env('MODEL_METADATA_CACHE_PREFIX', 'model_metadata'), ], ];
🎈 Usage
🔥 HasOneMetadata Trait
Add the HasOneMetadata trait to your model to enable a single metadata record:
use Waad\Metadata\Traits\HasOneMetadata; class Company extends Model { use HasOneMetadata; // <--- Add this trait to your model }
Some methods:
// Create metadata with array (only works if no metadata exists) $company->createMetadata(['key' => 'value', 'another_key' => 'another_value']); // Create metadata with collection $company->createMetadata(collect(['key' => 'value'])); // Update existing metadata $company->updateMetadata(['new_key' => 'new_value']); // Delete the metadata $company->deleteMetadata(); // Get metadata as array $metadata = $company->getMetadata(); // Get metadata as collection $metadataCollection = $company->getMetadataCollection();
🔥 HasManyMetadata Trait
Add the HasManyMetadata trait to your model to enable multiple metadata records:
use Waad\Metadata\Traits\HasManyMetadata; class Post extends Model { use HasManyMetadata; // <--- Add this trait to your model // Enabled Append id with content metadata (default) public $metadataNameIdEnabled = true; // Custom Append key of id with metadata (default) public $metadataNameId = 'id'; }
see Configuration Append Id for more details
Some methods:
// Create metadata with array or collection $post->createMetadata(['key1' => 'value1', 'key2' => 'value2']); $post->createMetadata(collect(['key1' => 'value1', 'key2' => 'value2'])); // Update metadata by ID $post->updateMetadata('{metadata_id}', ['new_key' => 'new_value']); // Delete metadata by ID $post->deleteMetadata('{metadata_id}'); // Get all metadata objects $metadata = $post->metadata; // or $metadata = $post->metadata()->get(); // Get metadata by ID $metadata = $post->getMetadataById('metadata_id'); // Get all metadata column pluck as array $allMetadata = $post->getMetadata(); // Get all metadata column pluck as collection $metadataCollection = $post->getMetadataCollection(); // Search in metadata $searchResults = $post->searchMetadata('search_term');
🗄️ Cache
The package includes an optional caching layer to reduce database queries. Cache is disabled by default and can be enabled in the config:
You can configure cache settings either in config/model-metadata.php or override them via your .env file.
| Config Key / Env Variable | Type | Default | Description |
|---|---|---|---|
cache.enabled / MODEL_METADATA_CACHE_ENABLED |
bool |
false |
Enable or disable metadata caching |
cache.ttl / MODEL_METADATA_CACHE_TTL |
int |
3600 |
Cache time-to-live in seconds |
cache.store / MODEL_METADATA_CACHE_STORE |
string|null |
null (empty) |
Cache store to use (null = default Laravel cache driver) |
cache.prefix / MODEL_METADATA_CACHE_PREFIX |
string |
model_metadata |
Prefix for all metadata cache keys |
For example, to enable cache and customize settings, add to .env:
MODEL_METADATA_CACHE_ENABLED=true MODEL_METADATA_CACHE_TTL=3600 MODEL_METADATA_CACHE_STORE=redis MODEL_METADATA_CACHE_PREFIX=model_metadata
When caching is enabled:
- Read operations (
getMetadata,getMetadataById,getMetadataCollection) are automatically cached. - Write operations (
create,update,delete,forget,sync) automatically invalidate the cache. - You can manually clear the cache for a specific model using
clearMetadataCache():
$company->clearMetadataCache(); $post->clearMetadataCache();
- You can check if caching is active:
$company->metadataCacheIsEnabled(); // bool
🧪 Testing
To run the tests for development, use the following command:
composer test
👨💻 Contributors
- Waad Mawlood
- Email: waad_mawlood@outlook.com
- Role: Developer
📝 License
This package is open-sourced software licensed under the MIT license.
waad/laravel-model-metadata 适用场景与选型建议
waad/laravel-model-metadata 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.54k 次下载、GitHub Stars 达 85, 最近一次更新时间为 2025 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「json」 「metadata」 「model」 「Flexible」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 waad/laravel-model-metadata 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waad/laravel-model-metadata 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 waad/laravel-model-metadata 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 4.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 85
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-03
