Spatie Media Library on a custom page

<?php

namespace App\Filament\Admin\Resources\CertificateResource\Pages;

use App\Filament\Admin\Resources\CertificateResource;
use App\Models\Certificate;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page;

class UploadCertificate extends Page implements HasForms
{
use InteractsWithForms;

public Certificate $record;

public array $data = [];

protected static string $resource = CertificateResource::class;

protected static string $view = 'filament.admin.resources.certificate-resource.pages.upload-certificate';

public function mount(Certificate $record): void
{
$this->record = $record;

$record->load(['device','media']);

$this->form->fill([
'serial_number' => $record->serial_number,
'device_id' => $record->device->ecu_number,
'certificate' => $record->getMedia(),
]);
}

public function form(Form $form): Form
{
return $form->schema([
Section::make('Certificate Copy')
->schema([
SpatieMediaLibraryFileUpload::make('media')
->label('Upload Scanned Certificate'),
]),
])->statePath('data')
->model($this->record);
}

public function submit(): void
{

}
}
<?php

namespace App\Filament\Admin\Resources\CertificateResource\Pages;

use App\Filament\Admin\Resources\CertificateResource;
use App\Models\Certificate;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page;

class UploadCertificate extends Page implements HasForms
{
use InteractsWithForms;

public Certificate $record;

public array $data = [];

protected static string $resource = CertificateResource::class;

protected static string $view = 'filament.admin.resources.certificate-resource.pages.upload-certificate';

public function mount(Certificate $record): void
{
$this->record = $record;

$record->load(['device','media']);

$this->form->fill([
'serial_number' => $record->serial_number,
'device_id' => $record->device->ecu_number,
'certificate' => $record->getMedia(),
]);
}

public function form(Form $form): Form
{
return $form->schema([
Section::make('Certificate Copy')
->schema([
SpatieMediaLibraryFileUpload::make('media')
->label('Upload Scanned Certificate'),
]),
])->statePath('data')
->model($this->record);
}

public function submit(): void
{

}
}
This is my code and I know I am missing something. All the documentation relating to Spatie media library is confusing. Can you someone help me what I have to do to upload and edit the image uploaded later. Submit method is empty coz i don't really know what to put here now.
7 Replies
christmex
christmex7mo ago
sorry, but what is actually happn? wht is the problem? did u try to upload and edit the image already? is there any error?
Karthick K
Karthick KOP7mo ago
When I try to access the data in submit method using $this->form->getState() I am always getting an empty array as the result Now with an empty array how can i update the data ?
Ramees
Ramees5mo ago
any update on this ?
Lara Zeus
Lara Zeus5mo ago
not sure, but media not in the fill array! is it a relationship?
Ramees
Ramees5mo ago
i managed that with a public variable in afterstateupadated now i need to show that is mount how to do that? @Lara Zeus how to show the uploaded image in edit of a custom page ? tried with mount giving full url
Ramees
Ramees5mo ago
i tried this but not working
Want results from more Discord servers?
Add your server