#Save Spatie Media Library File in Relationship

16 messages · Page 1 of 1 (latest)

dark shoal
#

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),
oak silo
#

Does your model have the necessary spatie media trait?

dark shoal
#

Hi @oak silo 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);
    }
}

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.

oak silo
#

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.

dark shoal
#

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.

oak silo
#

Yea. That’s what’s throwing me off too.

#

Is it a repo you can share?

dark shoal
#

Hi @oak silo 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

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

oak silo
#

I can pull it down tomorrow, but a quick glance at the code looks ok to me.

dark shoal
oak silo
#

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.

dark shoal
#

Ohh, gotcha, thanks anyway, I'll try to debug it and see if there is a bug in the package.

dark shoal
#

After many ways to handle this, I ended up with the solution!!