JSON Serialize
Model
and Collection
can be serialized recursively to JSON string. You can also deserialize them from JSON.
- Public and protected members will be serialized. Private members will not be included.
- Properties by getter and setter will not be included.
stdClass
also can be serialized, but only public members.
JSON Serialize
Serialize the Collection
$customers = Customer::all();
$json = $customers->toJson();
Result
{
"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"
},
}
}
Serialize the Model
$group = Group::find(1);
$json = $group->toJson();
Deserialize
Deserialize Collection or Model from JSON string
$json = $group->toJson();
$group = Model::fromJson($json);