/** * ExportAll the selected entries. * * @param TableBuilder $builder * @param ResponseFactory $response * @param array $selected */ public function handle(TableBuilder $builder, ResponseFactory $response, array $selected) { $model = $builder->getTableModel(); $stream = $builder->getTableStream(); $headers = ['Content-Disposition' => 'attachment; filename=' . $stream->getSlug() . '.csv', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Content-type' => 'text/csv', 'Pragma' => 'public', 'Expires' => '0']; $callback = function () use($selected, $model) { $output = fopen('php://output', 'w'); /* @var EloquentModel $entry */ foreach ($model->all() as $k => $entry) { if ($k == 0) { fputcsv($output, array_keys($entry->toArray())); } fputcsv($output, $entry->toArray()); } fclose($output); }; $builder->setTableResponse($response->stream($callback, 200, $headers)); }
/** * Return a new streamed response from the application. * * @param \Closure $callback * @param int $status * @param array $headers * @return \Symfony\Component\HttpFoundation\StreamedResponse * @static */ public static function stream($callback, $status = 200, $headers = array()) { return \Illuminate\Routing\ResponseFactory::stream($callback, $status, $headers); }