digitalruby/meta-data
最新稳定版本:1.0.0
Composer 安装命令:
composer require digitalruby/meta-data
包简介
Adds a flexible key/value table to your Laravel models
README 文档
README
Adds a flexible key/value table to your Laravel models.
Why this package?
I often find myself needing to store extra bits of information across entities in my apps. This could be a github link, maybe a SEO title or a path to a file.
Instead of expanding my database with new fields across multiple entities, I often resort back to a simple key value table that has a polymorphic relationship to any entity.
This package provides you with that table and a trait that you can add to your models to implement the functionality.
Installation
To install the package, use Composer:
composer require digitalruby/meta-data
After installing, you should run the migration to create the meta_data table in your database:
php artisan migrate
This command creates the necessary table for storing meta data related to your models.
Usage
-
Add the Trait to Your Model:
First, include the
HasMetaDatatrait in any Eloquent model you wish to associate meta data with.use DigitalRuby\MetaData\HasMetaData; class YourModel extends Model { use HasMetaData; // Model content }
-
Setting Meta Data:
To add or update meta data for a model, use the
setMetamethod.$model->setMeta('key', 'value');
key: The meta data key.value: The meta data value. Can be a string, array, or object. Arrays and objects are automatically encoded to JSON.
-
Getting Meta Data:
Retrieve a value with the
getMetamethod.$value = $model->getMeta('key');
Optionally, you can retrieve all meta data associated with the model:
$allMeta = $model->getAllMeta();
-
Deleting Meta Data:
To remove a meta data entry, use
deleteMeta.$model->deleteMeta('key');
This will delete the meta data entry with the specified key.
-
Advanced Usage:
The
getMetamethod supports additional query modifications via a callback function, allowing for more complex queries.
Requirements
- PHP 8 or higher
- Laravel 10 or higher
Contributing
We welcome contributions! Please submit pull requests for bug fixes, features, or improvements.
License
The DigitalRuby/MetaData package is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-24