jsefton/laravel-virtual-fields
Composer 安装命令:
composer require jsefton/laravel-virtual-fields
包简介
Allow eloquent models to store and retreive data against anything without needing a specific database column
README 文档
README
Allow eloquent models to store and retreive data against anything without needing a specific database column.
Virtual Fields hook into eloquent attribute fetching to allow it to use a specific field to map data but still support all the core features of eloquent and a hybrid of virtual and physical fields in a database table.
Installation
composer require jsefton/laravel-virtual-fields
Usage & Requirements
Virtual Fields is a trait that you can use in any eloquent model. e.g.
use Illuminate\Database\Eloquent\Model; use JSefton\VirtualFields\VirtualFields; class Post extends Model { use VirtualFields; }
For virtual fields to work the database table for the desired model must have a field called data. You should add this through a migration for each model / table you wish to use Virtual Fields.
php artisan make:migration add_virtual_field_data_table_to_{table} --table={table}
Once you have created a migration you will want something similar to the below to add the new field and handle removing in the down method.
/** * Run the migrations. * * @return void */ public function up() { Schema::table('{table}', function (Blueprint $table) { $table->longText('data')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('{table}', function (Blueprint $table) { $table->dropColumn(['data']); }); }
The reason for data field to be a longText is to support long term usage of Virtual Fields. If you keep adding new data to store via Virtual Fields it will end up with a large JSON object within this column. If you know it is only going to be used for a small amount of data then please make your migration use a suitable data type.
Example
Once setup you can simply use normal attribute getting & setting against a model. This will automatically be handled when being saved into the database againt the model and converted back to actual attributes on query.
In this example we have a Post and a virtual field of sub_title. This field does not exist as a physical field in the database schema.
$post = Post::find(1); // Set a virtual field of sub_title $post->sub_title = 'Example post sub title'; $post->save();
The sub_title will be saved in the data field mapped as an array with sub_title key and Example post sub title as a value.
When you want to get the value back out to use you simply use it like any normal eloquent attribute:
echo $post->sub_title; // outputs: Example post sub title
Please note currently for Laravel 7+ until tested and verified in lower versions.
TODO
- Add in support for querying easily against Virtual Fields without JSON query requirement.
jsefton/laravel-virtual-fields 适用场景与选型建议
jsefton/laravel-virtual-fields 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.43k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「schema」 「virtual」 「laravel」 「fields」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jsefton/laravel-virtual-fields 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jsefton/laravel-virtual-fields 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jsefton/laravel-virtual-fields 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Extension for Opis JSON Schema
Adds the EDTF data type to Wikibase
EAV modeling package for Eloquent and Laravel.
A simple library that allows transform any kind of data to native php data or whatever
serialize Symfony Forms into JSON schema
统计信息
- 总下载量: 1.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-03