php - fputcsv不能使用stdClass类型的对象作为数组
I use post method to download csv using php with fputcsv
when i using get method it works, but when using post it give me error, cannot use stdClass as array, people mention to use json_decode
but i'm not sure wher to place it,
I use laravel
CODE :
public function csv (Request $request) {
$storage_path = storage_path();
$from = date($request->from_date);
$to = date($request->to_date);
$table = DB::table('registrations')->whereBetween('created_at', [$from, $to])->get();
$filename = "data.csv";
$handle = fopen('php://output', 'w+');
fputcsv($handle, array('No', 'Nama', 'Nomor Telepon', 'Email', 'Tempat Tinggal',
'Bank', 'Kartu Kredit', 'Rumah', 'Mobil', 'Luar Negeri', 'Penghasilan'));
foreach($table as $row) {
fputcsv($handle, array( $row['id'], $row['name'], $row['phonenumber'], $row['email'],
$row['umur'], $row['tempattinggal'],
$row['bank'], $row['kartukredit'], $row['rumah'],
$row['keluarnegeri'], $row['penghasilan'] ));
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv',
);
return Response::download($filename, 'data.csv', $headers);
}
the error happen to be on foreach,when i remove the for each , i can donwload the csv, but in badly format
当我使用get方法时,我使用post方法使用带有 我使用laravel p>
CODE: p>
错误发生在foreach上,当我删除每个,我可以下载csv,但格式错误 p>
div> fputcsv code>的php下载csv 工作,但使用post它给我错误,不能使用stdClass作为数组,人们提到使用
json_decode code>但我不确定是否放置它, p>
public function csv(Request $ request){
$ storage_path = storage_path() ;
$ from = date($ request-> from_date);
$ to = date($ request-> to_date);
$ table = DB :: table('registrations') - > whereBetween('created_at',[$ from,$ to]) - > get();
$ filename =“data.csv”;
$ handle = fopen('php: //输出','w +');
fputcsv($ handle,array('No','Nama','Nomor Telepon','Email','Tempat Tinggal',
'Bank',' Kartu Kredit','Rumah','Mobil','Luar Negeri','Penghasilan'));
foreach($ table as $ row){
fputcsv($ handle,array($ row ['id '],$ row ['name'],$ row ['phonenumber'],$ row [' 电子邮件'],
$ row ['umur'],$ row ['tempattinggal'],
$ row ['bank'],$ row ['kartukredit'],$ row ['rumah'],
$ row ['keluarnegeri'],$ row ['penghasilan']));
}
fclose($ handle);
$ headers = array(
'Content-Type'=> 'text / csv',
);
返回Response :: download($ filename,'data.csv',$ headers);
}
code> pre>
I think the issue with your code is that when fetching the records from the database they are being returned as object and you are trying to use that object as if it was an array.
You have 2 solutions, solution 1 is the most efficient:
Solution 1:
Use the variable $row
as an object instead of an array
foreach($table as $row) {
fputcsv($handle, array( $row->id, $row->name, $row->phonenumber, $row->email,$row->umur, $row->tempattinggal,$row->bank, $row->kartukredit, $row->rumah,$row->keluarnegeri, $row->penghasilan ));
}
Solution 2:
Convert the $row
to an array for every loop
foreach($table as $row) {
$row = json_decode(json_encode($row), True);
//Or $row = (array)$row;
fputcsv($handle, array( $row['id'], $row['name'], $row['phonenumber'], $row['email'],$row['umur'], $row['tempattinggal'],$row['bank'], $row['kartukredit'], $row['rumah'],$row['keluarnegeri'], $row['penghasilan'] ));
}
I think this is what you need to do:
fputcsv($handle, json_encode(array($row['id'], $row['name'], $row['phonenumber'], $row['email'],
$row['umur'], $row['tempattinggal'],
$row['bank'], $row['kartukredit'], $row['rumah'],
$row['keluarnegeri'], $row['penghasilan'])));
The query builder get()
method returns a collection containing stdClass objects. You can convert that to an array with toArray()
(it will also convert the inner objects to arrays).
$table = $table->toArray();
If you like, you can also add a select('id', 'name', ...etc.)
to your query builder to specify the columns you want. If you do that, then you can write to the file with
fputcsv($handle, $row);
rather than building a new array there.
Also, I'm not sure about the way you're creating the download response. You're writing to php://output, which doesn't write to a file, but Response::download()
expects a path to a file. I think you either need to write the results to a temp file instead, or use streamDownload()
if you don't want to create a file locally.