Created
April 9, 2024 12:17
-
-
Save marty12321/8c8ea494aeabc586d454f9dc268e2e65 to your computer and use it in GitHub Desktop.
download not working for table in livewire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Livewire; | |
use App\Models\Directory; | |
use App\Models\Document; | |
use App\Services\DownloadService; | |
use Filament\Forms\Concerns\InteractsWithForms; | |
use Filament\Forms\Contracts\HasForms; | |
use Filament\Support\Enums\Alignment; | |
use Filament\Tables\Actions\Action; | |
use Filament\Tables\Columns\Summarizers\Sum; | |
use Filament\Tables\Columns\TextColumn; | |
use Filament\Tables\Concerns\InteractsWithTable; | |
use Filament\Tables\Contracts\HasTable; | |
use Filament\Tables\Table; | |
use Illuminate\Database\Eloquent\Relations\HasMany; | |
use Illuminate\Support\Facades\Storage; | |
use Livewire\Attributes\Locked; | |
use Livewire\Component; | |
use Filament\Tables\Actions\BulkAction; | |
class DirectoryFilesList extends Component implements HasForms, HasTable | |
{ | |
use InteractsWithTable; | |
use InteractsWithForms; | |
public $directory; | |
public $showDownloadButton = false; | |
public $documents = []; | |
#[Locked] | |
public $directory_id; | |
public function mount(Directory $directory) | |
{ | |
$this->directory = $directory; | |
$this->directory_id = $directory->id; | |
$this->documents = $directory->documents; | |
} | |
public function table(Table $table): Table | |
{ | |
return $table | |
->relationship(fn(): HasMany => $this->directory->documents()) | |
->columns([ | |
TextColumn::make('index') | |
->label('Lp.') | |
->rowIndex(), | |
TextColumn::make('description') | |
->searchable() | |
->sortable() | |
->label('Opis'), | |
TextColumn::make('attachment_original_name') | |
->searchable() | |
->sortable() | |
->label('Nazwa pliku'), | |
TextColumn::make('created_at') | |
->since() | |
->sortable() | |
->label('Data dodania'), | |
TextColumn::make('attachment_size') | |
->label('rozmiar pliku') | |
->formatStateUsing(fn(string $state): string => round($state / 1024, 2) . " kB") | |
->alignEnd(), | |
]) | |
->headerActions([ | |
Action::make('dodaj nowy plik do katalogu') | |
->icon('heroicon-s-plus') | |
->color('primary') | |
->hidden(fn() => $this->directory->is_locked) | |
->action(fn() => $this->redirect(route('upload', ['directory' => $this->directory]))), | |
]) | |
->actions([ | |
Action::make('pobierz') | |
->icon('heroicon-s-arrow-down-tray') | |
->action(fn(Document $document) => $document->download()), | |
Action::make('pobierz zip') | |
->icon('heroicon-s-arrow-down-tray') | |
->action(fn(Document $document) => $this->download()), | |
Action::make('usuń') | |
->requiresConfirmation() | |
->color('danger') | |
->icon('heroicon-s-trash') | |
->visible(fn(Document $document) => !$this->directory->is_locked) | |
->action(fn($record) => $record->delete()) | |
]) | |
->bulkActions([ | |
BulkAction::make('usuń zaznaczone') | |
->requiresConfirmation() | |
->action(fn($records) => $records->each->delete()) | |
->deselectRecordsAfterCompletion() | |
->visible(fn() => !$this->directory->is_locked) | |
->color('danger'), | |
BulkAction::make('pobierz zaznaczone') | |
->action(function ($records) { | |
ray($records); | |
$this->ZipFiles($records); | |
}) | |
->deselectRecordsAfterCompletion() | |
->color('success'), | |
]); | |
} | |
protected function ZipFiles($records) | |
{ | |
$zip = new \ZipArchive(); | |
$zipFileName = storage_path('app\\private\\') . 'pliki2.zip'; | |
$zip->open($zipFileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); | |
foreach ($records as $record) { | |
$zip->addFile(storage_path('app\\private\\' . $record->attachment), $record->attachment_original_name); | |
} | |
$zip->close(); | |
$this->showDownloadButton= true; | |
return $this->download(); | |
} | |
public function download() | |
{ | |
ray('download'); | |
if (Storage::disk('private')->exists('pliki2.zip')) { | |
return Storage::disk('private')->download('pliki2.zip', 'pliki2.zip'); | |
} | |
} | |
public function render() | |
{ | |
return view('livewire.directory-files-list'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment