Laravel 5.4查询生成器数组到字符串的转换

时间:2019-03-27 14:00:51

标签: php laravel

我正在尝试使用laravel 5.4命令从Excel导入数据。我可以收集所有数据,现在尝试插入数据库中,但出现一个奇怪的错误。

这是发生错误的一段代码:

$data = [
            'name' => $row['name'],
            'address' => $row['address'],
            'suite' => $row['suite'],
            'city' => $row['city'],
            'zipcode' => $row['zipcode'],
            'country_code' => $row['country_code'],
            'province_code' => $row['province_code'],
            'region_id' => $region_id,
            'phone' => $row['phone'],
            'fax' => $row['fax'],
            'website' => $row['website'],
            'linkedin' => $row['linkedin'],
            'facebook' => $row['facebook'],
            'twitter' => $row['twitter'],
            'number_employees' => $row['number_employees'],
            'year_creation' => $row['year_creation'],
            'turnover_id' => $turnover_id,
            'status' => 1,
            'hidden_directory' => 0,
            'hidden_directory_byadmin' => 0,
            'is_member' => $row['is_member'],
            'created_by' => 1,
        ];

        $company = Company::create($data);

这是错误:

   Array to string conversion (SQL: insert into `company` (`name`, `address`, `suite`, `city`, `zipcode`, `country_code`, `province_code`, `region_id`, `phone`, `fax`, `website`, `lin  
  kedin`, `facebook`, `twitter`, `number_employees`, `year_creation`, `turnover_id`, `status`, `hidden_directory`, `created_by`, `updated_at`, `created_at`) values (Test Company 3, 1  
  315 Rue St-Louis, , Montreal, H4L 2P4, CA, QC, , +1 (438) 999-555, +1 (438) 999-555, http://www.google.com, https://www.linkedin.com/feed/, https://www.facebook.com/, https://twitt  
  er.com/, 200, 2010, , 1, 0, 1, 2019-03-27 09:48:29, 2019-03-27 09:48:29)) 

这就像在SQL插入查询中丢失了对勾。奇怪的是,我在系统的其他部分进行了相同的插入,而这不会发生。

我不是要在此处插入JSON。是的,我在StackOverflow中已经看到了所有类似的问题,当您尝试插入json而不是常规字符串和整数时,通常会发生这种情况。

我在这里可能做错了什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

本质上,$row中的索引是array,而不是插入所期望的stringint。由于这可以是任何索引,因此最好使用带有try/catch逻辑的DB::transaction()块来避免发生此错误:

\DB::beingTransaction();

try {
    $data = [
        'name' => $row['name'],
        'address' => $row['address'],
        'suite' => $row['suite'],
        'city' => $row['city'],
        'zipcode' => $row['zipcode'],
        'country_code' => $row['country_code'],
        'province_code' => $row['province_code'],
        'region_id' => $region_id,
        'phone' => $row['phone'],
        'fax' => $row['fax'],
        'website' => $row['website'],
        'linkedin' => $row['linkedin'],
        'facebook' => $row['facebook'],
        'twitter' => $row['twitter'],
        'number_employees' => $row['number_employees'],
        'year_creation' => $row['year_creation'],
        'turnover_id' => $turnover_id,
        'status' => 1,
        'hidden_directory' => 0,
        'hidden_directory_byadmin' => 0,
        'is_member' => $row['is_member'],
        'created_by' => 1,
    ];

    $company = Company::create($data);

    \DB::commit();
} catch(\Exception $ex){
   \Log::error("Error in Company::create(): ".$ex->getMessage());
   \DB::rollBack();
}

如果不确定$row[...]可以包含什么,并且有可能使用array /否则为无效值,则使用try/catch这样可以防止插入问题,并且仅没有错误时,完成插入。除此之外,还要确保插入的数据符合您的期望。

答案 1 :(得分:-1)

您可以像这样对字符串进行样式转换

<svg viewBox="0 0 550 400" >

<!-- draw path and circles -->
<path id="hypecurve" d="M12.967,349.469c15.107-87.283,25.932-180.142,54.214-264.61c31.17-93.095,54.138,17.688,65.096,53.934c11.354,37.558,23.177,74.976,34.309,112.6c26.534,89.679,79.275-25.286,92.183-45.57c11.749-18.462,20.938-43.699,69.798-48.289c70.298-6.604,177.054-4.848,224.858-5.774" fill="none" stroke="#444" stroke-width="3"/>
<circle class= "circle1" id="c1" cx="0" cy="0" r="5" fill="#004" />


</svg>

<p>c1:<br><input type="range" id="itr1" min="0" max="100" value="0" step="1" /></p>

或者可以使用int / float代替字符串。这样会将您的数据保存为字符串格式。