【发布时间】:2017-06-29 18:20:41
【问题描述】:
所以我有一个具有 json 字段作为属性的模型。当它被检索到时,我想将该字段转换为一个数组,以便我可以用它做一些其他事情。
例如,如果 json 在名为 json 的参数中有类似 {name:bob,email:sue} 的数据,则在构建模型对象时,我希望它是这样的:
public function setJsonAttribute($value)
{
$this->attributes['json'] = $value;
$jsonInfo = json_decode($value, true);
$this->name = $jsonInfo['email'];
$this->email = $jsonInfo['name'];
}
这是我尝试使用 mutator 的一个示例,但它不是最优的。一旦模型加载了数据库中的数据,有什么方法可以自动完成这种事情吗?
Laravel 5.4
【问题讨论】:
标签: php json laravel model laravel-5.4