JSONシリアライズ
Model
およびCollection
は再帰的にJSON文字列にシリアライズ可能です。 JSON文字列から逆シリアライズすることも可能です。
- publicまたはprotectedのメンバのみシリアライズされます。privateメンバは含まれません。
- getterやsetterによるプロパティは含まれません。
stdClass
もシリアライズできますが、publicメンバのみが含まれます。
JSONシリアライズ
コレクションのシリアライズ
$customers = Customer::all();
$json = $customers->toJson();
結果
{
"className": "Transactd\\Collection",
"array": {
"0": {
"address": {
"zip": "123-2",
"address1": "3"
},
"address3": null,
"id": 3,
"name": "User3",
"className": "Customer",
"group": 3,
"option": 3,
"phone": 3,
"note": "",
"special_following": 2,
"update_at": "2016-11-18 16:29:53.855330"
},
}
}
モデルのシリアライズ
$group = Group::find(1);
$json = $group->toJson();
逆シリアライズ
JSON文字列からコレクションまたはモデルを逆シリアライズする
$json = $group->toJson();
$group = Model::fromJson($json);