Skip to content

Instantly share code, notes, and snippets.

@defrindr
Created July 31, 2025 14:24
Show Gist options
  • Select an option

  • Save defrindr/e5418b2124a1cb473a878110f320818f to your computer and use it in GitHub Desktop.

Select an option

Save defrindr/e5418b2124a1cb473a878110f320818f to your computer and use it in GitHub Desktop.
Impor data from excel in Laravel Filament
<?php
namespace App\Filament\Resources\TranslationResource\Pages;
use App\Filament\Resources\TranslationResource;
use App\Imports\TranslationsImport;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Forms\Components\FileUpload;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
class ListTranslations extends ListRecords
{
protected static string $resource = TranslationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Action::make('download_template')
->label('Download Template')
// ->icon('heroicon-o-download')
->url(route('translations.template.download'))
->openUrlInNewTab(),
Action::make('import')
->label('Import Translations')
->modalHeading('Import Translations via Excel')
->modalWidth('lg')
->form([
FileUpload::make('file')
->label('Excel File')
// ->rules(['required', 'file', 'mimes:xls,xlsx,csv'])
->required(),
])
->action(function (array $data) {
if (empty($data['file'])) {
// prevent file doesnt exist
return Notification::make()
->title('No file uploaded.')
->danger()
->send();
}
try {
// Access file from public path
$path = Storage::disk('public')->path($data['file']);
// Import into database
Excel::import(new TranslationsImport, $path);
// Notify user
return Notification::make()->title('Success upload file')->success()->send();
} catch (\Throwable $th) {
Log::error($th->getMessage());
return Notification::make()
->title('Error !')
->body($th->getMessage())
->danger()
->send();
}
}),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment