Jan 7, 2025
0
Level 3
Updating Media Attachments with Trix Editor, Livewire, and Spatie Media Library
Hi everyone! 👋
I'm currently working on a review system where users can write rich text content with image attachments using the Trix Editor. The initial file upload functionality works well with Livewire and Spatie Media Library but I'm encountering issues with the update functionality. Below is the code I'm working with:
class Edit extends Component
{
use WithFileUploads;
public Ranking $review;
public $notes;
public $fileAttachments = [];
public function getComponentFileAttachmentUrl(string $fileName): ?string
{
// Check if it's a new file upload
foreach ($this->fileAttachments as $file) {
if ($file->getClientOriginalName() === $fileName) {
return $file->temporaryUrl();
}
}
// Check existing media attachments
$existingMedia = $this->review->getMedia('ranking-attachments');
foreach ($existingMedia as $media) {
// Uncomment the line below to return the URL
// return $media->getUrl();
dd($media); // Debugging the media object
}
return null;
}
public function updateReview()
{
// Update review fields...
// Clear existing media attachments
$this->review->clearMediaCollection('ranking-attachments');
// Attach new uploaded files
foreach ($this->fileAttachments as $file) {
$this->review->addMedia($file)
->toMediaCollection('ranking-attachments');
}
}
}
Does anyone have suggestions on how to properly handle the update functionality, especially with maintaining compatibility with the Trix Editor and existing media attachments? Your help would be greatly appreciated. Thanks!
Please or to participate in this conversation.