【发布时间】:2021-10-06 22:33:57
【问题描述】:
您好,下面是我存储在数据库中的内容,但是当我在模型中使用 get 方法时,我必须在格式化数据时使用 json_decode 两次,为什么会发生这种情况,我可以让它以某种方式使用一次。
json 完全在 db 中:
"[{\"id\":\"1\",\"country\":\"New Zealand\",\"shipping_rate\":\"1\"},{\"id\":\"2\",\"country\":\"Australia\",\"shipping_rate\":\"2\"}]"
模型获取方法:
public function getshippingAttribute()
{
return $this->attributes['shipping'] ? json_decode(json_decode($this->attributes['shipping'])) : [];
}
【问题讨论】: