I'm trying to add a download button in a extended Custom Field
Hello, I'm trying to add a download button for a media that use Spatie Media Library, I have the function, but i can't use it in my blade
when i use wire:click="download", it goes to the main page file, and i get an error because obviously, the function doesn't exists there, but in the Custom Field file
<?php
namespace App\Forms\Components;
use Closure;
use Filament\Forms\Components\Field;
use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Str;
use Carbon\Carbon;
class DocumentViewer extends Field
{
protected string $view = 'forms.components.document-viewer';
protected Closure|Media $document;
public function document(Closure|Media $document): static
{
$this->document = $document;
return $this;
}
public function getUrl(): ?string
{
$media = $this->evaluate($this->document);
if ($media instanceof MediaCollection) {
/** @var Media $document */
$document = $media->first();
return $document->getTemporaryUrl(Carbon::now()->addMinutes(5));
}
return null;
}
public function download()
{
$media = $this->evaluate($this->document);
if ($media instanceof MediaCollection) {
/** @var Media $document */
return response()->download($media->first()->getPath(), $media->first()->file_name);
}
return null;
}
public function getType(): ?string
{
$media = $this->evaluate($this->document);
if ($media instanceof MediaCollection) {
/** @var Media $document */
$document = $media->first();
return $document->getTypeFromMime();
}
return null;
}
public function getDocumentId(): string
{
return 'document-' . Str::random(12);
}
}
<?php
namespace App\Forms\Components;
use Closure;
use Filament\Forms\Components\Field;
use Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Str;
use Carbon\Carbon;
class DocumentViewer extends Field
{
protected string $view = 'forms.components.document-viewer';
protected Closure|Media $document;
public function document(Closure|Media $document): static
{
$this->document = $document;
return $this;
}
public function getUrl(): ?string
{
$media = $this->evaluate($this->document);
if ($media instanceof MediaCollection) {
/** @var Media $document */
$document = $media->first();
return $document->getTemporaryUrl(Carbon::now()->addMinutes(5));
}
return null;
}
public function download()
{
$media = $this->evaluate($this->document);
if ($media instanceof MediaCollection) {
/** @var Media $document */
return response()->download($media->first()->getPath(), $media->first()->file_name);
}
return null;
}
public function getType(): ?string
{
$media = $this->evaluate($this->document);
if ($media instanceof MediaCollection) {
/** @var Media $document */
$document = $media->first();
return $document->getTypeFromMime();
}
return null;
}
public function getDocumentId(): string
{
return 'document-' . Str::random(12);
}
}
1 Reply
Did you manage to resolve this?