Search code examples
phplaravellaravel-livewire

How do I store an uploaded file in the correct directory using Laravel Livewire 3?


I have this page that uploads the user's avatar to the the app. I'm using Livewire 3, so it should be simple.

// validation
$user->avatar = $this->photo->store('avatars');
$user->save();

Now, this works, but instead of storing the file in /storage/app/public/avatar it uploads it to /storage/app/avatar so I can't see the files in the browser. Field in the database has the hash and file extension, so it's working, and if I go directly to the directory, of course they're there.

This won't work:

<img src="/storage/{{ Auth::user()->avatar }}">

neither

<img src="{{ asset('/storage/') . Auth::user()->avatar }}">

But if I manually move the /avatar directory inside /storage/app/public, it works.

This must be a configuration issue for sure, but I don't know what. I don't find it in the Laravel documentation or Livewire's, it should be working like magic, but it's not.

Thanks!


Solution

  • Check your config/filesystems.php, there is an array with default key that indicate your default disk, and a disks key, that contains your available disks and their information as an array, (you can also add yours). Since you said your files were uploaded at /storage/app/avatar, I guess you are using defualt disk, local. you can change default disk to public which store files to storage_path('app/public') or change your code to

    $user->avatar = $this->photo->store(path: 'avatars', 'public');
    

    Read more
    https://livewire.laravel.com/docs/uploads#storing-uploaded-files https://laravel.com/docs/10.x/filesystem#the-local-driver