marmarait / laravel-dynamic-attributes
为您的模型添加动态属性
v1.02
2018-08-31 18:32 UTC
This package is not auto-updated.
Last update: 2024-09-29 05:42:10 UTC
README
概述
使用键值表为您的模型添加额外字段
安装
composer require marmarait/laravel-dynamic-attributes
用法
-
将“HasDynamicAttributes”特质添加到您的模型中
-
在“getDynamicAttributes”方法中返回一个包含动态属性及其字段类型的数组。可用类型包括:
- 字符串
- 文本
- 整数
- 双精度浮点数
- 对象
- 日期
- 时间
- 日期时间
-
像使用静态属性一样使用它。
示例:如果想要您的 Users 模型有一个名为 "country" 的额外字段,类型为字符串:将 HasDynamicAttributes 添加到您的 User 模型中,使 getDynamicAttributes 方法返回 ['country'=>'string']
通过分配属性给模型来设置字段
$user->country='Austria';
获取属性的方式大致相同
echo $user->country; // 'Austria'