Tricks

Add action to Table to export record as JSON file

Jun 18, 2022
David VINCENT
Table builder
Tables\Actions\Action::make('exportAsJson')
->label(__('Export'))
->action(function ($record) {
$name = Str::slug($record->name, '_');
return response()->streamDownload(function () use ($record) {
$return = $record->attributesToArray();
echo json_encode($return, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
}, $name . '.json');
})
->tooltip(__('Export'))
->icon('heroicon-s-download')
->color('primary'),
avatar

I tried this trick, but I got error Class "App\Filament\Resources\InvSaranaResource\Pages\Tables\Actions\Action" not found

What class should I import to fix this?