laravel insert查询值为空

时间:2018-01-22 07:09:27

标签: php mysql laravel

 <?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`......

我觉得价值空..... 我所做的? 感谢

1 个答案:

答案 0 :(得分:0)

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

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

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

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