SpatieMediaLibraryFileUpload on a related record

I have a User model and a Customer model, they have a one-to-one relationship with each other. User model implements the Spatie\MediaLibrary\HasMedia and uses the Spatie\MediaLibrary\InteractsWithMedia trait. Using SpatieMediaLibraryFileUpload component in a form in User resource works fine, it loads the media when you're in the Edit page. For the Customer resource, it doesn't load on edit page even when I put a ->relationship('user') in a layout form component like Section.
// CustomerForm.php
Section::make('Customer ')
->relationship('user')
->schema([
SpatieMediaLibraryFileUpload::make('avatar')
->collection('avatar')
->avatar()
->columnSpanFull(),
]),
// CustomerForm.php
Section::make('Customer ')
->relationship('user')
->schema([
SpatieMediaLibraryFileUpload::make('avatar')
->collection('avatar')
->avatar()
->columnSpanFull(),
]),
My current solution for this is :
// EditCustomer.php
protected function getHeaderActions(): array
{
$isFirstLoad = empty($this->data['user']['avatar']);
$avatar = $this->record->user->getFirstMedia('avatar')?->uuid;

if ($isFirstLoad && $avatar) {
data_set($this->data['user'], 'avatar', [$avatar => $avatar]);
}

return [
ViewAction::make(),
DeleteAction::make(),
];
}
// EditCustomer.php
protected function getHeaderActions(): array
{
$isFirstLoad = empty($this->data['user']['avatar']);
$avatar = $this->record->user->getFirstMedia('avatar')?->uuid;

if ($isFirstLoad && $avatar) {
data_set($this->data['user'], 'avatar', [$avatar => $avatar]);
}

return [
ViewAction::make(),
DeleteAction::make(),
];
}
I was wondering if there's a more "native" approach for this.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?