SpatieMediaLibraryFileUpload inside createOptionForm()

Hello,

I have an exception when I save record with SpatieMediaLibraryFileUpload inside createOptionForm => Call to undefined method App\Models\MealItems::getMedia()

Explanation :

I have a form to save a meal. Inside it, I have a repeater to insert many dishes.
To do that I use
Forms\Components\Repeater::make('items')->schema([
Forms\Components\Select::make('dish_id')->options(Dish::pluck('name', 'id'))->createOptionForm([
    ...
SpatieMediaLibraryFileUpload::make('media')
        ->label('Image')
        ->collection('dishes-images')
        ->image()
])
->createOptionUsing(fn ($data) => Dish::create($data)->getKey())
])

When I create new dish by clicking + button, a modal opens, all fields are saved in dishes table after submit, and this record is set to dish_id select of meal form but not my image in media table.
Then when I try to edit this meal, back to create a new dish clicking + button, and after submit button modal, I get this error Call to undefined method App\Models\MealItems::getMedia().

In fact is normal since HasMedia is in Dish model and not MealItems

Forms\Components\Repeater::make('items') items is the name of my relationship

How do I say to spatie to get ID of Dish created to permit to save image in media table ?

Thank you
Was this page helpful?