marshmallow/metadata
Composer 安装命令:
composer require marshmallow/metadata
包简介
A package to simply add metadata to models
README 文档
README
Metadata
A package to simply add metadata to any Eloquent model with a simple cast. Metadata is stored in a single related table as JSON, keyed per model, so you can attach arbitrary attributes to a model without adding extra columns.
Installation
You can install the package via composer:
composer require marshmallow/metadata
You can publish and run the migrations with:
php artisan vendor:publish --tag="metadata-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="metadata-config"
This is the contents of the published config file:
return [ /* * The fully qualified class name of the metadata model. */ 'metadata_model' => Marshmallow\Metadata\Models\Metadata::class, /* * The fully qualified class name of the metadata cast. */ 'metadata_cast' => Marshmallow\Metadata\Casts\MetadataCast::class, /* * Automatically eager load metadata to prevent N+1 queries. * Set to false if you prefer to manually eager load metadata. */ 'eager_load' => false, ];
Configuration
| Key | Default | Description |
|---|---|---|
metadata_model |
Marshmallow\Metadata\Models\Metadata::class |
The model used to store metadata. Swap it to use your own. |
metadata_cast |
Marshmallow\Metadata\Casts\MetadataCast::class |
The cast that maps a model attribute to metadata storage. |
eager_load |
false |
When true, the metadata relation is automatically eager loaded on models using the trait to prevent N+1 queries. |
Usage
Add the HasMetadata trait to your model:
use Marshmallow\Metadata\Traits\HasMetadata; class Example extends Model { use HasMetadata; }
Add a cast for every field you want to store as metadata:
use Marshmallow\Metadata\Casts\MetadataCast; protected $casts = [ 'example_field' => MetadataCast::class, ];
After which you can get & set your field using normal model attribute access:
$example_model->example_field = 'This is an example'; $example_model->save(); $example_field = $example_model->example_field; // 'This is an example'
Array and object values are automatically encoded to JSON on write and decoded on read:
$example_model->example_field = ['key' => 'value']; $example_model->example_field; // ['key' => 'value']
When you set metadata on a model that has not been persisted yet, the value is queued and written automatically once the model is created. Metadata is also automatically deleted when the model itself is deleted.
Working with the metadata relation directly
Each model that uses the trait exposes a metadata morph-one relation backed by the Metadata model, which offers a few helpers:
// Read or write a single key $example_model->metadata->getMetadata('example_field'); $example_model->metadata->addMetadata('example_field', 'value'); // Remove a single key $example_model->metadata->removeKey('example_field'); // Clear all metadata for the model $example_model->metadata->clear();
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please report security vulnerabilities by email rather than via the public issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
marshmallow/metadata 适用场景与选型建议
marshmallow/metadata 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.57k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「metadata」 「laravel」 「marshmallow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marshmallow/metadata 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marshmallow/metadata 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marshmallow/metadata 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This packages provides you with the ability to easily connect your Laravel application to ZohoDesk.
Generate slugs when saving Eloquent models
A Laravel Nova help field.
Connect your Kiyoh to your Laravel application
Sending request to you Google Analytics has never been easier. With support for e-commerce tracking.
A Laravel Nova field to prefill fields based on another field. Super Awesome!
统计信息
- 总下载量: 4.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-07