我有包含JSON在我得爲對象 但是當我使用laravel模型然後將數據它顯然返回JSON或字符串,我必須每一個地方,在那裏我用這個模型解碼列的表(這是不幹燥的概念),但事情是,如果我拉一個記錄,一個地方,那麼它的代碼,並確定只有一個執行,但如果我要拉的數據和多個地方的集合,然後我要運行相同的解碼什麼每次都是在有任何laravel方式,可以爲我做的,而不是使用自制的循環如何使用laravel5將數據mysql中的json數據作爲對象?
下面是表
+-----------------------------------------------------------------------------+
| table |
+-----------------------------------------------------------------------------+
|id | title | text |
+-----------------------------------------------------------------------------+
| 1 | A1 | {"subject":"Subject1","word":"Lorem ipsum dolor sit amet, ..."} |
| 2 | A2 | {"subject":"Subject2","word":"Lorem ipsum dolor sit amet, ..."} |
| 3 | A3 | {"subject":"Subject3","word":"Lorem ipsum dolor sit amet, ..."} |
| 4 | A4 | {"subject":"Subject4","word":"Lorem ipsum dolor sit amet, ..."} |
| 5 | A5 | {"subject":"Subject5","word":"Lorem ipsum dolor sit amet, ..."} |
| 6 | A6 | {"subject":"Subject6","word":"Lorem ipsum dolor sit amet, ..."} |
| 7 | A7 | {"subject":"Subject7","word":"Lorem ipsum dolor sit amet, ..."} |
| 8 | A8 | {"subject":"Subject8","word":"Lorem ipsum dolor sit amet, ..."} |
| 9 | A9 | {"subject":"Subject9","word":"Lorem ipsum dolor sit amet, ..."} |
| 10| A10 | {"subject":"Subject10","word":"Lorem ipsum dolor sit amet..."} |
+-----------------------------------------------------------------------------+
我已經嘗試了本
$tables = TableModel::all();
foreach ($tables as &$table) {
$table->text = json_decode($table->text);
}
結果就是我想要
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => A1
[text] => stdClass Object
(
[subject] => Subject1
[word] => Lorem ipsum dolor sit amet...
)
)
[1] => stdClass Object
(
[id] => 2
[title] => A2
[text] => stdClass Object
(
[subject] => Subject2
[word] => Lorem ipsum dolor sit amet...
)
)
...
)
如果有什麼簡單的方法即是更有效,並按照幹 然後跟我分享。
在此先感謝。
我叫過它然後循環模式,並使用'json_decode對象覆蓋那麼,元件()' – User97798