Laravel Excel 不能将 stdClass 类型的对象用作数组
Laravel Excel Cannot use object of type stdClass as array
我不得不使用 DB::select
而不是调用模型,因为我的数据结构太复杂了。
我的主要 SQL 查询是;
/* main mysql query */
$sql = "SELECT t1.* FROM `contracts`.`payments` as t1 LEFT JOIN `postcodes`.`" . $postcode_dataset . "` AS t2 ON t1.`VendorZIP` = t2.`postcode` WHERE ";
foreach ($unserialized_input as $key => $value) {
$sql .= "t2." . $key . " IN ('".implode("', '", $unserialized_input[$key])."') OR ";
}
$sql = rtrim($sql, " OR ");
$payments = DB::select($sql);
我正在尝试使用 Maatwebsite Excel 导出结果;
Excel::create('SME_Payments_Export-U', function($excel) use ($payments) {
$excel->sheet('Excel sheet', function($sheet) use ($payments) {
$sheet->setOrientation('landscape');
$sheet->fromArray($payments);
});
})->export('csv');
但是我得到了错误;
Cannot use object of type stdClass as array
这是我的示例 var_dump;
array(176) {
[0]=>
object(stdClass)#302 (4) {
["Fiscal Year"]=>
string(5) "13/14"
["Contract Number"]=>
string(0) ""
["Foreign Ind"]=>
string(1) "N"
["Valued or Running"]=>
string(1) "R"
}
[1]=>
object(stdClass)#304 (4) {
["Fiscal Year"]=>
string(5) "13/14"
["Contract Number"]=>
string(0) ""
["Foreign Ind"]=>
string(1) "Y"
["Valued or Running"]=>
string(1) "V"
}
[2]=>
object(stdClass)#305 (4) {
["Fiscal Year"]=>
string(5) "13/14"
["Contract Number"]=>
string(0) ""
["Foreign Ind"]=>
string(1) "N"
["Valued or Running"]=>
string(1) "R"
}
如何转换 payments
?
根据 documentation,您需要先将对象数组转换为数组数组,然后再将其传递给 fromArray()
方法:
foreach ($payments as &$payment) {
$payment = (array)$payment;
}
$sheet->fromArray($payments);
我遇到过同样的问题,最适合我的是:
$payments = json_decode( json_encode($payments), true);
这会将 object/array 转换为数组并使其可用。
我不得不使用 DB::select
而不是调用模型,因为我的数据结构太复杂了。
我的主要 SQL 查询是;
/* main mysql query */
$sql = "SELECT t1.* FROM `contracts`.`payments` as t1 LEFT JOIN `postcodes`.`" . $postcode_dataset . "` AS t2 ON t1.`VendorZIP` = t2.`postcode` WHERE ";
foreach ($unserialized_input as $key => $value) {
$sql .= "t2." . $key . " IN ('".implode("', '", $unserialized_input[$key])."') OR ";
}
$sql = rtrim($sql, " OR ");
$payments = DB::select($sql);
我正在尝试使用 Maatwebsite Excel 导出结果;
Excel::create('SME_Payments_Export-U', function($excel) use ($payments) {
$excel->sheet('Excel sheet', function($sheet) use ($payments) {
$sheet->setOrientation('landscape');
$sheet->fromArray($payments);
});
})->export('csv');
但是我得到了错误;
Cannot use object of type stdClass as array
这是我的示例 var_dump;
array(176) {
[0]=>
object(stdClass)#302 (4) {
["Fiscal Year"]=>
string(5) "13/14"
["Contract Number"]=>
string(0) ""
["Foreign Ind"]=>
string(1) "N"
["Valued or Running"]=>
string(1) "R"
}
[1]=>
object(stdClass)#304 (4) {
["Fiscal Year"]=>
string(5) "13/14"
["Contract Number"]=>
string(0) ""
["Foreign Ind"]=>
string(1) "Y"
["Valued or Running"]=>
string(1) "V"
}
[2]=>
object(stdClass)#305 (4) {
["Fiscal Year"]=>
string(5) "13/14"
["Contract Number"]=>
string(0) ""
["Foreign Ind"]=>
string(1) "N"
["Valued or Running"]=>
string(1) "R"
}
如何转换 payments
?
根据 documentation,您需要先将对象数组转换为数组数组,然后再将其传递给 fromArray()
方法:
foreach ($payments as &$payment) {
$payment = (array)$payment;
}
$sheet->fromArray($payments);
我遇到过同样的问题,最适合我的是:
$payments = json_decode( json_encode($payments), true);
这会将 object/array 转换为数组并使其可用。