O'Reilly logo

Learning Laravel 4 Application Development by Hardik Dangar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exporting orders

The next task in our queue is to allow Jordan to export new orders that are paid in the Excel format so that he can print Foldagram easily as per new orders.

public function get_csvexport()
{
 
    $output = Foldagram::with('foldagram_reff_address')
                ->where('foldagram_orders.status','=',1)
                ->where('foldagram.status','=',2)
                ->where('foldagram.exported','=',0)->get();

    $file = fopen('dummy/path/file.csv', 'w');
    foreach ($output as $row) {
        fputcsv($file, $row->to_array());
    }
    fclose($file);

    $headers = array(
        'Content-Type' => 'text/csv',
        'Content-Disposition' => 'attachment; filename="$file"',
     );

    return Response::make($file, 200, $headers);
}

The preceding code will first gather data via Eloquent loading method and save data into the file ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required