更新时间:2023-01-09 10:09:54
数组中的水合模型
Hydrate model from array
$data = '{
"unique_id_001":{"name":"John","email":"JD@***.com"},
"unique_id_002":{"name":"Ken","email":"Ken@***.com"}
}';
$object = (array)json_decode($data);
$collection = \App\User::hydrate($object);
$collection = $collection->flatten(); // get rid of unique_id_XXX
/*
Collection {#236 ▼
#items: array:2 [▼
0 => User {#239 ▶}
1 => User {#240 ▶}
]
}
*/
dd($collection);