我正在使用 guzzle 包從 API 接收 JSON 數(shù)據(jù)。我要存儲在數(shù)據(jù)庫中的接收數(shù)據(jù):JSON數(shù)據(jù)我的數(shù)據(jù)保存到數(shù)據(jù)庫代碼:$contents = json_decode($response->getBody());$object = new Product();$object->GeneralInfo = $contents->data->GeneralInfo; $object->save(); return response()->json($contents);我收到的異常:stdClass 類的對象無法轉換為字符串我知道它想要存儲正在傳遞的字符串和對象。存儲這些數(shù)據(jù)的最佳方式是什么?轉換為數(shù)組可能嗎?提前感謝您的提示。
1 回答

陪伴而非守候
TA貢獻1757條經(jīng)驗 獲得超8個贊
來自內(nèi)容的一般信息是對象。當 laravel 嘗試將其插入數(shù)據(jù)庫時,它會嘗試將數(shù)組轉換為字符串。因此,當它獲取對象時,它無法將其轉換為字符串。
將您的響應正文解碼為數(shù)組: $contents = json_decode($response->getBody(), true);
然后從數(shù)組中獲取數(shù)據(jù): $object->GeneralInfo = $contents['data']['GeneralInfo'];
- 1 回答
- 0 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消