我正在尝试使用 Laravel Eloquent 将整个 API 响应插入到 MySQL 表中,但出现“数组到字符串转换”错误。我该如何解决这个问题?
请注意,我必须保存整个 API 响应。
我的 API 调用
$response = Curl::to($url.$request->account_number)
->withData($data)
->asJson(true)
->get();
我的查询
TransactionLog::create([
'payer' => $request->payer,
'amount' => $request->amount,
'phone' => $request->phone,
'response' => $response
]);
最佳答案
看起来你在使用 ixudra/curl ?
使用 asJson(true)
你会收到 json_decode
响应,所以如果你的计划是在数据库中保存原始 json 响应,你必须 json_encode
就像:
TransactionLog::create([
'payer' => $request->payer,
'amount' => $request->amount,
'phone' => $request->phone,
'response' => json_encode($response)
]);
关于mysql - 如何使用 Laravel ORM 在 MySQL 表中插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47314728/