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

david001's avatar

Unable to display image in vue component form storage of Laravel

Hi developers, I am unable to display the image in vue component from the Storage directory of Laravel. Please help me to solve this issue:

images are stored in DB as public/avatar/TSD50oeYXnkrX1nQ38N9zljP1FRdP42yPhJOrEKg.png

if I remove public path and do this <img :src="'../storage/avatar/31deG4gnbO3EwO2s26lnUe0KzJjskFfm8lPIGFeM.jpeg'" width="100">

i can see image diplayed in vue component from storage/public/avatar folder

method to get details of single user


public function user($id){
		return $user = User::find($id);
	}

methods to save image


public function profilePic(Request $request){
		$this->validate($request,[
			'image'=>'required|mimes:jpeg,jpg,png'
		]);
		$image = $request->image->store('public/avatar');
		$authUser = auth()->user()->id;
		$user = User::where('id',$authUser)->update([
			'profilePic'=> $image
		]);
		return redirect()->back();
	}

vue:



<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Profile Component</div>

			{{user.profilePic}}
//how can i display image here
 			

<div class="card-body">
                        <form @submit.prevent="updateProfilePic" method="post" enctype="multipart/form-data">
                            <input type="file" name="image" class="form-control"v-on:change="onImageChange">
                            <button type="submit" class="btn btn-primary">Submit</button>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        props:['userid'],
        data(){
            return{
                image:'',
                user:[]

            }
        },
        mounted() {
            this.getUser()
            console.log('Component mounted.')
        },
        methods:{
            onImageChange(e){
                console.log(e)
                this.image = e.target.files[0];
                
                },
            updateProfilePic(){
                const config={
                    headers:{
                        "content-type":"multipart/form-data"
                    }
                }
                let formData = new FormData();
                formData.append('image',this.image);
             
                axios.post('/profile-pic',formData,config).then((response)=>{
                    this.image='';
                    this.getUser()
                   
                   

                }).catch((error)=>{
                    console.log(error)
                    this.allerrors = error.response.data.errors
                })
            },
            getUser(){
                axios.get('/user/'+this.userid).then((response)=>{
                    this.user = response.data
                }).catch((error)=>{
                    alert('some error')
                })
            },
        }
    }
</script>
0 likes
3 replies
tefo's avatar

well, there is two ways one is simple just create symlink in public path for your storage. and second is just get base_64 encoded image from storage when you return user information (appending or just add in controller) like this: User.php

protected $appends = ['profile_base'];

public function getProfileBaseAttribute() {
    return base64_encode(Storage::get($this->profile)); //whatever field u saved
}

in your controller no need to change but the component where you want to show image look like this


<div class="card-header">Profile Component</div>

<img :src="user.profile_base" />
// display image here
 			

<div class="card-body">

thats it

2 likes
MaverickChan's avatar

store images in the public folder is fine , you don't have to protect image files , because they are meant to be seen.

Zaeem Syed's avatar

i have displayed this using ​ <img :src="'/storage/'+ (user.profilePic.substring(7))" alt=""> ​

1 like

Please or to participate in this conversation.