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.
Group::make()
->relationship('car')
->schema([
TextInput::make('mileage')
->label(__('Mileage'))
->numeric()
->suffix(__('km.'))
->required(),

TextInput::make('license_plate')
->label(__('License Plate'))
->unique('cars', 'license_plate', ignoreRecord: true)
->live()
->afterStateUpdated(function (HasForms $livewire, TextInput $component) {
$livewire->validateOnly($component->getStatePath());
})
->required(),

SpatieMediaLibraryFileUpload::make('images')
->label(__('Images'))
->multiple()
->image()
->required(),
])
->columns(3)`
->columnSpan(3),
Group::make()
->relationship('car')
->schema([
TextInput::make('mileage')
->label(__('Mileage'))
->numeric()
->suffix(__('km.'))
->required(),

TextInput::make('license_plate')
->label(__('License Plate'))
->unique('cars', 'license_plate', ignoreRecord: true)
->live()
->afterStateUpdated(function (HasForms $livewire, TextInput $component) {
$livewire->validateOnly($component->getStatePath());
})
->required(),

SpatieMediaLibraryFileUpload::make('images')
->label(__('Images'))
->multiple()
->image()
->required(),
])
->columns(3)`
->columnSpan(3),
12 Replies
awcodes
awcodes3mo ago
Does your model have the necessary spatie media trait?
Garrita
GarritaOP3mo ago
Hi @awcodes thanks for answering, yes it does. Parent (Car.php)
class Car extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;

public function auctionCar(): HasOne
{
return $this->hasOne(AuctionCar::class);
}
}
class Car extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;

public function auctionCar(): HasOne
{
return $this->hasOne(AuctionCar::class);
}
}
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.
No description
No description
No description
awcodes
awcodes3mo ago
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.
Garrita
GarritaOP3mo ago
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.
awcodes
awcodes3mo ago
Yea. That’s what’s throwing me off too. Is it a repo you can share?
Garrita
GarritaOP3mo ago
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.
Garrita
GarritaOP3mo ago
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
awcodes
awcodes3mo ago
I can pull it down tomorrow, but a quick glance at the code looks ok to me.
Garrita
GarritaOP3mo ago
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...
awcodes
awcodes3mo ago
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.
Garrita
GarritaOP3mo ago
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!!
Garrita
GarritaOP3mo ago
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...
Want results from more Discord servers?
Add your server