首页 > 解决方案 > 使用 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()

标签: phpmysqllaravel

解决方案


你的问题是。您尝试在插入时将数组分配给字符串:

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')

            ]
        );
    }

}

推荐阅读