FilamentF
Filament3y 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;
}


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)?
Was this page helpful?