5

I am using Laravel Livewire and Spatie media library and I couldn't find anything helpful on both of their documentation on how to do this.

am trying this but it gave me error

$this->menu->addMediaFromUrl($this->menu_image->temporaryUrl())->toMediaCollection('menu_image');

am getting this error error am getting

kenean50
  • 518
  • 5
  • 13
  • Welcome to Stack Overflow! Please see [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/a/284237/4575350) To learn more about this community and how we can help you, please start with the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) and its linked resources. – STA Aug 17 '20 at 16:04
  • Pls add some code so that we can at least take a look! – Muhammad Tariq Aug 17 '20 at 17:36
  • just updated my question, maybe you can help now – kenean50 Aug 17 '20 at 18:02

4 Answers4

6

For me it works like this:

public $uploadedItems;
    
public function updatedUploadedItems($uploadedFile)
{
    $item = Item::create();

    $item
        ->addMedia($uploadedFile->getRealPath())
        ->usingName($uploadedFile->getClientOriginalName())
        ->toMediaCollection('images');
}
Sebastian
  • 97
  • 7
3

Since my storage and temporary folder are both on s3, the answers did not work for me.
Here's one that worked for me

use Spatie\MediaLibrary\Support\RemoteFile;

...

return $gallery->addMedia($photo->getRealPath())
            ->setFile(new RemoteFile($photo->getRealPath(), 's3'))
            ->usingName($photo->getClientOriginalName())
            ->toMediaCollectionOnCloudDisk('default');

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
  • 1
    Thanks, this works for me, though I can't quite get conversions and responsive images to work consistently. – duffn Jan 24 '22 at 17:02
1

This is the only way it worked for me after testing

      $this->menu
           ->addMediaFromString($this->menu_image->get())
           ->usingFileName($this->menu_image->getFilename())
           ->toMediaCollection('menu_image');
Havilah
  • 11
  • 1
0

I've tried all the above options but none seems to work for me so i dug deeper and this is the solution working for me. It's more like a hack but it worked for me.

$this->lecture
      ->addMediaFromDisk('livewire-tmp/'.$this->lectureVideo->getFilename())
      ->toMediaCollection();

Maybe it has to do with working environment but getRealPath() throws file not not found exception.