amazingacademy
amazingacademy

Reputation: 143

create csv download for table results

I need to create a csv file with a download button for my table results view. I am using Laravel framework and am unsure on how exactly to do this.

My controller to get the data.

    public function reportFailedPayments(){
    $startdate = Input::get('startdate');
    $enddate = Input::get('enddate');
    $status = Input::get('status');
    $data = Payment::with('purchase', 'history', 'history.billingInformation', 'purchase.user', 'purchase.productInstance', 'purchase.productInstance.product', 'billingInformation')
            ->where('Status', '=',$status)->whereBetween('Payment_Date', array($startdate, $enddate))->orderBy('Payment_Date','desc')->get();
    return View::make('admin.reports.failedPayments.report', array('payments'=>$data));
}

}

And here is my table view. Just needing some advice on how exactly to create the DL for the csv.

<table class='list'>
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Email</th>
<th>Product Name</th>
<th>Purchase Date</th>
<th>Initial Payment Date</th>
<th>Last Payment Date</th>
<th>Billing Method</th>
<th>Transaction Code</th>
<th>Transaction Message</th>
</tr>
</thead>
<tbody>
@foreach($payments as $p)
   <tr>
   <td>{{ $p->purchase->user->Last_Name.', '.$p->purchase->user->First_Name }}</td>
   <td>{{ $p->purchase->user->Email }}</td>
   <td>{{ $p->purchase->productInstance->product->Name }}</td>
   <td>{{ $p->purchase->Purchase_Date  }}</td>
   <td>{{ $p->Payment_Date  }}</td>
   <td>{{ $p->lastHistory()->Process_Time }}</td>
   <td>{{ $p->lastHistory()->billingInformation->Payment_Type == PaymentType::PayPal ?     "PayPal" :
   CreditCardType::ToString($p->lastHistory()->billingInformation->CCType)." Ending In     ".$p->lastHistory()->billingInformation->CCLast4 }}</td>
   <td>{{ $p->lastHistory()->Trans_Response_Code }}</td>
   <td>{{ $p->lastHistory()->Trans_Response_Text }}</td>
   </tr>
@endforeach
</tbody>
</table>

Upvotes: 0

Views: 1115

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152900

Let me start by saying that I won't write all the code for you. This just isn't the purpose of StackOverflow...
However I hope I can point you in the right direction with this answer.
Your problem can be split in two tasks. One being generating the CSV file and then serve the download.

Generating the CSV file

You'll find a lot of code snippets on the internet. Here's one I found and modified a bit

$csvData = $yourCollection->toArray();
$filename = tempnam(sys_get_temp_dir(), 'csv_'); // create a temporary file in the system's default tmp directory
$file = fopen($filename, 'w');
foreach($csvData as $row){
    fputcsv($file, $row, ';'); // 3rd parameter is optional (default: ',')
}
fclose($file);

Now we have a csv file in our temp folder, ready to be downloaded. Laravel gives you a very way to download files.

Serving the file for download

In your controller action:

$response = Response::download($filename);
unlink($filename); // lets not forget to delete the file or they will pile up in your temp folder
return $response;

Maybe you also need to send some additional headers (e.g. for specifying a filename that the user sees) but I'll leave that up to you.

And here are some references to the functions used in the code:

Upvotes: 1

Related Questions