Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raobilal4822's avatar

Image Error in spatie media in relation

I am in filament resource form and i have a repeater with relation but i have a form in the that repeater that creating a user data that is guranter type saving in database. for images and documents i am using spatie media pacakge for adding that in media table. but now i have a form in the repeater (tenancy_tenant) the form is directly creating the user guranteer but when i submit that form spatie media files saving with the model type tenancy tenant but i want user model because i am creating user not tenancy tenany in the repeater here below the code

0 likes
1 reply
LaryAI's avatar
Level 58

The issue you're experiencing is that when you use SpatieMediaLibraryFileUpload inside a Filament repeater with a relationship, the uploaded media files are being attached to the parent model (e.g., TenancyTenant) instead of the intended User model (Guarantor), because Filament's relationship forms attach media to the model being created/edited by the repeater, not to any nested model you might create inside a modal or custom action.

How to solve this:

You need to manually move or attach the uploaded media to the correct User model after you create the Guarantor user. The SpatieMediaLibraryFileUpload component provides a way to get the uploaded files, and you can then attach them to the newly created user.

Step-by-step Solution

1. Do NOT use saveRelationshipsUsing in the FileUpload fields.

Let the files be uploaded to temporary storage, and handle the actual attachment in your action.

2. After creating the Guarantor user, attach the uploaded files to the user.

Update your action callback for the create_guarantor action like this:

3. Make sure your FileUpload fields are set to store files on disk, not directly to a model.

For example:

SpatieMediaLibraryFileUpload::make('id_document')
    ->label('ID Document')
    ->collection(MediaCollectionEnum::DOCUMENTS->value)
    ->acceptedFileTypes(['image/jpeg', 'image/png', 'application/pdf'])
    ->disk('local') // or your desired disk
    ->directory('tmp') // optional: a temp directory
    ->columnSpanFull(),

Do not use saveRelationshipsUsing here.


Summary

  • Do not let Filament automatically attach media to the parent model in the repeater.
  • After creating the Guarantor user, manually attach the uploaded files to the user using Spatie's addMedia() method.
  • Get the file paths from $data and move them from temp storage to the user's media collection.

Extra: Clean Up Temporary Files

If you want, after attaching the files, you can delete the temp files from storage.


Let me know if you need a more detailed example or if you want to handle multiple files per field!

Please or to participate in this conversation.