1

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

mandaputtra
  • 922
  • 3
  • 18
  • 36
  • 1
    `DB::table()` appears to be returning an object rather than an associative array - if you want an array, if it is possible to configure the database connection to fetch arrays, do so, otherwise, you should be able to just cast it to an array `$table = (array)$table;` or rather in the loop: `$row = (array)$row;` – Michael Berkowski Oct 22 '18 at 15:47
  • Possible duplicate of [Convert PHP object to associative array](https://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array) – Nigel Ren Oct 22 '18 at 15:56

3 Answers3

0

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'])));
0

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'] ));
}
Omar Tanti
  • 1,368
  • 1
  • 14
  • 29
0

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80