0

I am trying to fing how we can integrate Livewire with spatie/laravel-medialibrary in Laravel. In this code I can get the image information when user submit the form, but I can't save an uploaded image with spatie/laravel-medialibrary:

class ProfileComponent extends Component
{
    use WithFileUploads;

    public $photo;

//...

public function submit()
{
    $this->validate($this->rules);

    $user = User::find(auth()->user()->id);

    $user->name = $this->name;
    $user->family = $this->family;
    $success = $user->save();


    //...
    try {
        $user
            ->addMedia($this->photo)
            ->toMediaCollection(User::MEDIA_COLLECTION_NAME);
    } catch (FileDoesNotExist | FileIsTooBig $e) {
        dd($this->photo);
    }
}

or I try to use this code, but I get an error too

->addMediaFromDisk('/livewire-tmp/'.$this->photo->getFilename())

here we don't have request and by default livewire used store function for uploading images. how can we integrate them?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
DolDurma
  • 15,753
  • 51
  • 198
  • 377

1 Answers1

0

use this code

$user->addMedia($this->photo->getRealPath())->toMediaCollection('Colle');
Joundill
  • 6,828
  • 12
  • 36
  • 50