定制 liam-wiltshire/laravel-model-meta 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

liam-wiltshire/laravel-model-meta

Composer 安装命令:

composer require liam-wiltshire/laravel-model-meta

包简介

Metadata support for Eloquent

README 文档

README

liam-wiltshire/laravel-model-meta is an extension to the default Laravel Eloquent model to add metadata to a model.

What MetaData?

While Eloquent is very good at handling relational data, however not all data we deal with works like this. Without going as far as using a NoSQL solution such as Mongo, using mySQL to hold relational data, but with a JSON meta field is a potential solution.

liam-wiltshire/laravel-model-meta allows you to use standard Eloquent getters and setters to add and remove metadata to your models. Any attributes that relate to a column in your database table will be handled as a standard attribute, but anything else will be added to the meta data.

This gives you great flexibility to have structured data where appropriate (for example an book will always have a title, author etc), but to then have other related, unstructured data (for example some books might have the number of pages, others might not)

When you are structuring your data, it's important to consider if you want to be able to query this data. Unless you use something like virtual columns at the DB level, this data isn't queryable - it's not designed to store the primary data for that record.

Example

$book = new \App\Models\Book();

$book->forceFill(
[
    'title' => 'Laravel Quickstart Guide',
    'author_id' => 90,
    'publisher_id' => 5,
    'description' => 'This is an awesome book'
]
);

$book->save();

$book = \App\Models\Book::find(1);

//This is an actual field in the DB, so this will be set to that attribute
$book->title = 'Laravel Quickstart Guide, Second Edition';

//This isn't a field in the DB, and isn't a relationship etc, so will be stored in the meta field
$book->page_count = 200;

$test->save();

This code would generate a new Test model and save it to the DB. Assuming that meta_subject is not a column in our table, the meta_subject will automatically be added to the metadata:

root@localhost:[homestead]> SELECT id, title, meta FROM books;
+----+------------------------------------------+--------------------+
| id | title                                    | meta               |
+----+------------------------------------------+--------------------+
|  1 | Laravel Quickstart Guide, Second Edition | {"page_count":200} |
+----+------------------------------------------+--------------------+

Installation

liam-wiltshire/laravel-model-meta is available as a composer package: composer require liam-wiltshire/laravel-model-meta

Once installed, use the \LiamWiltshire\LaravelModelMeta\Concerns\HasMeta trait in your model.

The database table behind the model will need a meta column adding - by default the trait assumes this will be called meta:

$table->json('meta')->nullable()->default(null);

If the name of your meta column is different, then add a $metaDbField property to your model containing the name of the field:

protected $metaDbField = 'metaData';

Migration & Compatibility

Version Compatibility

Current Version (v1.0+)

For Laravel 11 & 12 with PHP 8.2+

composer require liam-wiltshire/laravel-model-meta:^1.0

Requirements:

  • PHP ^8.2
  • Laravel ^11.0|^12.0
  • PHPUnit ^10.0|^11.0 (dev)

Legacy Version (v0.5.x)

For Laravel 5.5-10 with PHP 7.1+

composer require liam-wiltshire/laravel-model-meta:^0.5

Requirements:

  • PHP ^7.1|^8.0
  • Laravel ^5.5|^6.0|^7.0|^8.0|^9.0|^10.0
  • PHPUnit ^7.0 (dev)

Note: If you're running an older version of Laravel or PHP, continue using v0.5.x. Version 1.0+ includes breaking changes related to minimum PHP and Laravel version requirements.

liam-wiltshire/laravel-model-meta 适用场景与选型建议

liam-wiltshire/laravel-model-meta 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.76k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 liam-wiltshire/laravel-model-meta 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 liam-wiltshire/laravel-model-meta 我们能提供哪些服务?
定制开发 / 二次开发

基于 liam-wiltshire/laravel-model-meta 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 49.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-22