F
Filament14mo ago
Mdk

Filling a SpatieMediaLibraryImageEntry field programmatically on a new Model (not saved yet)

I'm trying to set up a "cloning" functionality where the user can pick an existing Item and make a copy that he can edit before saving it as his own I made a copy of the CreateRecord page that accept an extra param (the ID of the item to be cloned) and added this
protected function afterFill(): void
{
$item = Item::findOrFail($this->item);
$this->data['status'] = 'enabled';
$this->data['title'] = $item->title;
$this->data['description'] = $item->description;
}
protected function afterFill(): void
{
$item = Item::findOrFail($this->item);
$this->data['status'] = 'enabled';
$this->data['title'] = $item->title;
$this->data['description'] = $item->description;
}
So far so good, but my Items also have images attached, via the SpatieMediaLibrary plugin, and I can't find a way to bring the original item's images in the form I thought about persisting the item to the database and then using the EditRecord page but that would be extra clutter in case the user doesn't actually save his item anymore Any other way to auto-fill that field from existing images (that would then have to be copied over to new Model as well)?
5 Replies
awcodes
awcodes14mo ago
I’m afraid you’d have to duplicate the images too and then attach them to the new record. I could be wrong but I’m fairly certain spatie media library doesn’t support attaching an existing image to multiple models.
Mdk
MdkOP14mo ago
I was hoping that creating a TemporaryUploadedFile and passing it the path of the original item's image would've worked, but while I do get a temporary path that exists on the disk, the uploader field doesn't pick it up and shows nothing If i were to instantiate the new Model on before/afterFill, and override the ModelCreation method as well, how could I listen for a "discarded" item to delete? unless I set some new extra property on it, that is, and run a cleaner every night or something
awcodes
awcodes14mo ago
Unless i'm misunderstanding you, livewire will automatically clean up the temp folder of anything older than 24 hours when ever a new file is uploaded. You could also just set a secheduled task to clean it up every 24 hours.
Mdk
MdkOP13mo ago
while that's true, i wouldn't be "uploading" it per se but cloning it in the final destination folder for this collection, since it's not an upload
awcodes
awcodes13mo ago
Yea. Then just set up a scheduled task to empty the tmp folder over night.
Want results from more Discord servers?
Add your server