Add export functionality to the generated index page.
// In Livewire Index
public function export(string $format = 'csv'): StreamedResponse
{
return response()->streamDownload(function () use ($format) {
$query = Product::query()->with(['user'])->when($this->search, ...);
if ($format === 'csv') {
$file = fopen('php://output', 'w');
fputcsv($file, ['Name', 'Price', 'User']);
foreach ($query->cursor() as $product) {
fputcsv($file, [$product->name, $product->price, $product->user?->name]);
}
fclose($file);
}
}, "products-".now()->format('Y-m-d').".csv");
}
Description
Add export functionality to the generated index page.
Proposed Syntax
php artisan crudify:generate Product --fields="name:string,price:decimal" --exportFeatures
maatwebsite/excelorspatie/simple-excel)Generated Code