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())
])
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
14 Replies
Dan Harrin
Dan Harrin2y ago
sounds like you need to add HasMedia and InteractsWithMedia to the MealItem model since the files get attached to the new select option
yagrasdemonde
yagrasdemondeOP2y ago
Hi Dan, I created a repository to explain in example what happens
yagrasdemonde
yagrasdemondeOP2y ago
GitHub
GitHub - agencetwogether/filament-fork
Contribute to agencetwogether/filament-fork development by creating an account on GitHub.
yagrasdemonde
yagrasdemondeOP2y ago
do i need open an issue on your repo linking mine ?
awcodes
awcodes2y ago
As Dan said you’re not using the HasMedia trait or the InteractsWithMedia on your MealItem model. That’s not a Filament issue. I’d recommend reading through the docs for spatie’s media library package.
yagrasdemonde
yagrasdemondeOP2y ago
hum no i'm not agree MealItem record relation, here I want to create a new dish with createoption and so it is the Dish model that must be associated with the image that I send not relationship please look my repo, and you'll see what I mean
awcodes
awcodes2y ago
I looked at your repo and that model is not adhering to how spatie’s package works. Just do what Dan said and you should be fine. Spatie’s package requires those on your model to work because it uses polymorphic relations. If you don’t add those to your model then it won’t work.
yagrasdemonde
yagrasdemondeOP2y ago
i tried to add InteractsWithMedia and HasMedia on MealItems model and in create scenario, when i create a new dish throught creating new meal, not working, no save in media table nor public folder. And in edit meal scenario, when I create new dish, I have a record with modelType MeaItems in media table and file in public folder, but it's not an image of my Dish item. Do you see the problem ? If you create a new Dish directly no problem, media is attached to Dish model in media table
awcodes
awcodes2y ago
So wrap your media field in a group with a relationship to media. Sounds like a relationship is off somewhere.
yagrasdemonde
yagrasdemondeOP2y ago
in a group ? like Forms\Components\Group::make() ?
Dan Harrin
Dan Harrin2y ago
yeah but i think you are misunderstanding the media library package a bit if you are creating a new meal item, i dont understand why you would want the media for that item to be attached to the meal instead in that case, why have the upload in the modal at all when it could be on the meal form
yagrasdemonde
yagrasdemondeOP2y ago
I think we are not on the same wavelength 😄 . I want to show in front meal details . See below
yagrasdemonde
yagrasdemondeOP2y ago
yagrasdemonde
yagrasdemondeOP2y ago
I display meal title, image title and others fields belonging to this meal, and with repeater, I choose differents dishes for each dish I display fields belonging of it like title, description etc and image or it In Filament I don't want to attach this (of dish) image to meal when I create a new dish with createOption method from meal form the problem occurs is when I create a Dish with DishResource, image is saved in database and folder, so relationship works. But when I create a new Dish throught Meal form, MealResource (with createOptionForm()), no image is saved nor in folder as if spatie doesn't understand to take this Dish and use relationship
Want results from more Discord servers?
Add your server