luisferfranco's avatar

How do I store a file in the correct directory using 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!

0 likes
3 replies
shariff's avatar
shariff
Best Answer
Level 51

You need to pass second argument as public.

$this->photo->store('avatars','public');

For more info you can check laravel docs https://laravel.com/docs/10.x/filesystem#specifying-a-disk

1 like
luisferfranco's avatar

@shariff Thanks it works, but I don't get the documentation.

There's no reference to (..., 'public') in Livewire's, and in Laravel docs I understood that was for things like AWS' S3

I was following this video: https://www.youtube.com/watch?v=1L6pBSo7LqQ around 10'30" author copies the Livewire's example and the files got stored where they should, TBH I didn't watch the video from start, maybe there's one configuration to tell Livewire/Laravel where to store files by default

Thanks a lot!

shariff's avatar

@luisferfranco It is not about Livewire. It is a laravel storage configurations. In laravel by default they are different disks local, public, s3. For more information you can see the config file config/filesystems.php.

1 like

Please or to participate in this conversation.