Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zachleigh's avatar

Laravel 5.3 and saving avatars to the storage directory

So in the docs is says that you can now easily save files to the storage directory. The example given is for an avatar:

    public function update(Request $request)
    {
        $path = $request->file('avatar')->store('avatars');

        return $path;
    }

So if you store your avatars to the local storage directory, how do you get them out? Ive been trying to use the Storage facade, but it either returns nothing, freezes up completely, or says it cant find my file. Not quite sure whats going on here. This is my controller logic:

    public function update(Request $request)
    {
        $user = Auth::user();

        if ($request->file('userPhoto')) {
            $userPhoto = $request->file('userPhoto')->store('photos/user');
            // $userPhoto = Storage::putFile('photos/user', $request->file('userPhoto'), 'public');

            $profile = UserProfile::firstOrCreate(['user_id' => $user->id()]);

            $profile->user_photo = $userPhoto;

            $profile->save();
        }

        return back()->with(['username' => $user->username()]);
    }

And heres the code in the view where I try to access it:

<img src="{{ Storage::get($user->profile->userPhoto()) }}">

The path is being stored in the database and the relationship works fine. Only when I try to use Storage to retrieve the photo do run into trouble. I know that generally things in the storage directory are not visible publicaly, but I am under the impression that by using the Storage class, I can allow access to certain items, like an avatar. Am I wrong here?

0 likes
7 replies
SaeedPrez's avatar
Level 50

Best way is to create a symlink to your storage folder and serve them that way, Laravel has an artisan command for this (doc) but I prefer to do it myself..

ln -s /path/to/storage/folder /path/to/public/folder

Example code..

        // Save image
        // This will save the files in /storage/app/uploads folder
        if ($request->hasFile('image')) {
            $model->image = $request->file('image')->store('uploads');
        }

I have a symlink from /public/img/uploads to uploads in my storage folder.

        @unless(is_null($model->image))
            <img src="{{ url('/img/' . $model->image) }}">
        @endif
zachleigh's avatar

Thanks @SaeedPrez I just figured that out :) You can create the link with artisan:

php artisan storage:link
1 like
YanTao's avatar
$path = $request->file('avatar')->store('avatars');

        return $path;

when i use this function it has an error Call to a member function store() on null

zachleigh's avatar

You probably arent receiving the file correctly. What does your form look like?

steveb's avatar

If you're using vagrant/Homestead, ensure you're running php artisan storage:link while SSH'd into the virtual machine. That got it for me.

mhyeganeh's avatar

@YanTao

Make sure to add enctype="multipart/form-data" attribute to your form element in html !

Otherwise in can not send the image file and this error shows up...

Please or to participate in this conversation.