【问题标题】:laravel insert query value emptylaravel 插入查询值为空
【发布时间】:2018-01-22 07:09:27
【问题描述】:
 <?xml version="1.0" encoding="utf-8"?>
<elenco_clienti>
 <cliente>
  <cod>00001</cod>
  <name>xxxxxxxxxx</name>
  <address>VIA nnnnnnnn</address>
</cliente>
<cliente>
 <cod>00003</cod>
 <name>yyyyyyyyyyy</name>
 <address>VIA calllll</address>
</cliente>
<cliente>
 <cod>00005</cod>
 <name>oooooooooooo</name>
 <address></address>
</cliente>
</elenco_clienti>

我有这个 Xml 文件,我的数组是这样的:

 $xml1 = simplexml_load_file($path);
 $json = json_encode($xml1);
 array = json_decode($json, true);
 $clien =$array['cliente'];

但是当我提出查询时

 if(!empty($clien))
 {
     DB::table('clientis')->insert($clien);
     dd('Insert Recorded successfully.');
 }

我有错误

QueryException Array to string conversion (SQL: insert into `clientis`......

我认为值是空的..... 我所做的? 谢谢

【问题讨论】:

  • 你确定$clien 是一个数组吗?如果将 if 语句更改为 if(!empty($clien) &amp;&amp; is_array($clien)) 会发生什么

标签: php mysql laravel


【解决方案1】:

您收到此错误是因为最后一项中的 address 是一个空数组:

2 => array:3 [▼
    "cod" => "00005"
    "name" => "oooooooooooo"
    "address" => []
]

你需要做这样的事情来解决这个问题:

$clien = array_map(function($i) {
    $i['address'] = empty($i['address']) ? '' : $i['address'];
    return $i;
}, $clien);

【讨论】:

  • 但我需要在其他地方修复其他空值,而不仅仅是这个。我的数组有很多空值
  • @NuCarvignulu 我给了你一个修复所有空地址的解决方案。
  • 但空值不仅在地址@Alexey Mezenin
  • @NuCarvignulu 只需为 array_map() 闭包内的任何其他列添加相同的代码。
猜你喜欢
  • 2016-04-04
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2019-12-05
  • 1970-01-01
  • 2016-02-08
  • 2013-01-07
相关资源
最近更新 更多