【发布时间】:2020-12-06 05:38:00
【问题描述】:
我尝试在将集合返回以使用 maatwebsite Excel 导出之前对其进行修改。我试图修改的列是 created_date 列(laravel 的默认 created_date),所以当导出到 excel 时,我只得到日期而不是日期时间。
我试过了:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
$temp = Invoice::all();
$len = count($temp);
for($i=0;$i<$len;$i+=1){
$temp[$i]->created_at = $temp[$i]->created_at->format('m/d/Y')
}
return $temp;
}
}
在控制器中:
public function export()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
但是,当导出时,excel文件中的结果是,例如:'07/26/2016T00:00:00.000000Z'
我注意到时间变为零,当我尝试时:
$temp[$i]->created_at = "some random string"
网页中的laravel返回错误说“一些随机字符串”不能被Carbon Class构造函数解析为日期时间。
如何使导出不使用我在“created_at”列中给出的字符串构造日期时间,而只返回纯字符串?如果让我们说,我不能修改数据库所以我不能创建额外的列和额外的列对于这个简单的事情太多了,我想。
【问题讨论】:
标签: excel laravel-5 php-carbon