Save Spatie Media Library File in Relationship
I have a form resource where I save the child and the parent but when I try to save the files for the parent it does not store them, just the other attributes except the files.
Models: AuctionCar belongsTo Car
AuctionCarResource.php
In this case, I save the mileage and the license plate for the parent, but it does not save the images.
12 Replies
Does your model have the necessary spatie media trait?
Hi @awcodes thanks for answering, yes it does.
Parent (Car.php)
In the Create it does not work but then when I am redirected to the Edit page and I upload again the image, it works.
Hmm. I think the relationship might be off. Ie your code is car.images with images being the relationship to spatie. But your model is a relationship for auctionCar. Doesn’t explain why it works on edit though.
Just my gut feeling though based on what you’ve shared. I could be wrong though.
the resource is for the child AuctionCarResource, so in the form I create it along with the parent (Car), so that's why I use the relationship function in the group component, but for some reason the spatie media library is not working on create, just edit, I've researched in issues, pr, and documentation of course but nothing found.
Yea. That’s what’s throwing me off too.
Is it a repo you can share?
Hi @awcodes I've just created it
https://github.com/javierpomachagua/filament-demo
just clone it
composer install
php artisan filament:user
and that's all.
There are User and Profile models (Profile belongsTo User and User hasOne Profile).
The avatar image is from the User so when I created it in the ProfileResource using Group and relationship does not work, I only save the attributes for User except the files.
GitHub
GitHub - javierpomachagua/filament-demo
Contribute to javierpomachagua/filament-demo development by creating an account on GitHub.
I'll be really glad to you to test it
I've tested it using just FileUpload and it works, but SpatieMediaLibrary does not work
I can pull it down tomorrow, but a quick glance at the code looks ok to me.
GitHub
SpatieMediaLibraryFileUpload not working within Fieldset or Group r...
Package filament/spatie-laravel-media-library-plugin Package Version v3.2 Laravel Version v.11.9 Livewire Version v3.0.0 PHP Version PHP 8.3 Problem description I have a User and a Profile model wi...
I think the problem here is that, although you are trying to save the image from profile to the releated user, but Spatie is dependent on the active record. So, I think it's trying to save with 'profile' as the associate model instead of 'user'. but profile doesn't have the media interface or trait and it's just gracefully failing.
Honestly not sure if this is a bug in the plugin or not. I would expect it to work, as you have. Sorry, I don't have a solid answer for you.
Ohh, gotcha, thanks anyway, I'll try to debug it and see if there is a bug in the package.
After many ways to handle this, I ended up with the solution!!
here my explanation
https://github.com/filamentphp/filament/issues/14246
GitHub
SpatieMediaLibraryFileUpload not working within Fieldset or Group r...
Package filament/spatie-laravel-media-library-plugin Package Version v3.2 Laravel Version v.11.9 Livewire Version v3.0.0 PHP Version PHP 8.3 Problem description I have a User and a Profile model wi...