php - 使用 Laravel 将 JSON 文件上传到 MYSQL 不起作用
问题描述
我正在编写一个脚本,使用 laravel 将包含我们所有公司数据的 JSON 文件从我们以前的 CRM 系统上传到 MYSQL 数据库中,我遇到了解析 json 数据的问题。我已经将它转换为一个数组并运行一个 foreach 来上传每个公司,但是它抛出一个错误,说它不能从数组转换为字符串。我不确定我在这里做错了什么,我的代码和一家公司都在下面(都非常相似):
public function uploadCompanies()
{
$json = File::get("database/data/accounts.json");
$data = json_decode($json);
foreach($data as $item) {
DB::table('ucustomers')->insert(
[
'dynamics_guid' => $item->accountid,
'customer_code' => $item->mc_unleashedcode,
'customer_name' => $item->name,
'phone_number' => $item->telephone1,
'fax_number' => $item->fax,
'email' => $item->emailaddress1,
'website' => $item->websiteurl,
'override_reps' => [],
'updated_at' => date('Y-m-d H:i')
]
);
}
}
帐户.json
[
{
"@odata.etag": "W/\"145746454\"",
"name": "Core Pharmacy",
"opendeals": 0,
"modifiedon": "2020-08-29T14:11:19Z",
"address1_stateorprovince": "VIC",
"telephone1": "03 9338 1504",
"accountid": "5b46f158-d8ef-e911-a812-000d3a799888",
"websiteurl": null,
"mc_unleashedcode": "C1000096",
"fax": "03 9334 5030"
"emailaddress1": "tullamarine@locale.com.au",
}
]
错误
ErrorException
Array to string conversion
at vendor/laravel/framework/src/Illuminate/Support/Str.php:488
484|
485| $result = array_shift($segments);
486|
487| foreach ($segments as $segment) {
> 488| $result .= (array_shift($replace) ?? $search).$segment;
489| }
490|
491| return $result;
492| }
+8 vendor frames
9 app/Unleashed/DynamicsUpload.php:52
Illuminate\Database\Query\Builder::insert()
10 app/Console/Commands/InsertDynamicsData.php:41
App\Unleashed\DynamicsUpload::uploadCompanies()
解决方案
你的问题是。您尝试在插入时将数组分配给字符串:
public function uploadCompanies()
{
$json = File::get("database/data/accounts.json");
$data = json_decode($json);
foreach($data as $item) {
DB::table('ucustomers')->insert(
[
'dynamics_guid' => $item->accountid,
'customer_code' => $item->mc_unleashedcode,
'customer_name' => $item->name,
'phone_number' => $item->telephone1,
'fax_number' => $item->fax,
'email' => $item->emailaddress1,
'website' => $item->websiteurl,
'override_reps' => [], // error on this one
'updated_at' => date('Y-m-d H:i')
]
);
}
}
有 2 个解决方案可以修复它。
如果要将数组直接插入到表中,则需要对其进行编码,如下所示:
public function uploadCompanies()
{
$json = File::get("database/data/accounts.json");
$data = json_decode($json);
foreach($data as $item) {
DB::table('ucustomers')->insert(
[
'dynamics_guid' => $item->accountid,
'customer_code' => $item->mc_unleashedcode,
'customer_name' => $item->name,
'phone_number' => $item->telephone1,
'fax_number' => $item->fax,
'email' => $item->emailaddress1,
'website' => $item->websiteurl,
'override_reps' => json_encode([]), // need to encode first
'updated_at' => date('Y-m-d H:i')
]
);
}
}
解决方案 2 使用模型进行创建:
但首先您需要将 override_reps 字段转换为数组。所以 laravel 会为你处理
// assume you model name Customer
class Customer extends Model
{
protected $casts =[
'override_reps'=>'array',
];
}
// on controller
public function uploadCompanies()
{
$json = File::get("database/data/accounts.json");
$data = json_decode($json);
foreach($data as $item) {
Customer::create(
[
'dynamics_guid' => $item->accountid,
'customer_code' => $item->mc_unleashedcode,
'customer_name' => $item->name,
'phone_number' => $item->telephone1,
'fax_number' => $item->fax,
'email' => $item->emailaddress1,
'website' => $item->websiteurl,
'override_reps' => [],//lavavel model will encode it before insert
'updated_at' => date('Y-m-d H:i')
]
);
}
}
推荐阅读
- c++ - 将 adobe xd 文件导入 c++ 项目
- c# - 通过 COM 调用 .NET 方法,使用 ref 数组
- python - 为什么 Beautifulsoup 不从该页面返回所需的项目?
- firebase - 如何从 BigQuery 中提取 Firebase 每日用户参与度?
- python - 如何根据年或周按数据框分组以及如何在python中组合两个数据集
- r - 您如何使用 ifelse 引用 R dplyr mutate_at / mutate_if 函数中的多个列?
- rust - 您如何正确实现视图矩阵?
- sql - 如何在按不同记录分组时以十五分钟的时间间隔计算记录
- reactjs - 即使父组件已“重新加载”,最后一个子组件也会渲染 iframe
- android - 将 android 矢量可绘制 XML 转换为 SVG