2016-11-15 72 views
0

是否有任何理由能言善辩$casts wouldnt工作?Laravel JSON数组铸造

我存储JSON,JSON列类型,并

protected $casts = [ 
    'fields' => 'array' 
]; 

但是...

​​

仍返回JSON

"{"first_name":"Brian","last_name":"Dillingham","email":"[email protected]"}" 

当我变异

甚至不工作
public function getFieldsAttribute($value) 
{ 
    return json_decode($value, true); 
} 

但工作的时候我

dd(json_decode(\App\Lead::find(1)->fields, true)); 
+0

我认为它,因为我手动存储在文本字段的JSON,对不起,不能删除的问题 –

回答

1

只要输入您的mutator功能将它转换:

public function getFieldsAttribute($value) 
{ 
    return (array) json_decode($value, true); 
}