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

Spiral's avatar

How to send image link from Laravel storage to frontend(angular)

public function index(){
        $data = HeaderSlider::all();
        return $data;
    }

  <ul *ngFor="let item of data">
    <img [src]="item.image"  />
  </ul>
0 likes
27 replies
Spiral's avatar

Thansk for replying but i want to sent a complete url in end point include image path bcs front end dont know where it hosted

Snapey's avatar

I know

and I dont know how you have stored the link to the image in your database

1 like
Spiral's avatar
private function uploadProfilePicture(Request $request)
    {
        try {
            $file = $request->file('profile_picture');
	        if(!empty($file) && $file != null)
	        {
	            $user = getApiUser();    
        		$user_profile = User::whereId($user->id)->first();

	            $fileExentsion              	= $file->extension();
	            $fileName                   	= $file->getClientOriginalName();
	            $fileFullName               	= time()."_".$fileName;
	            $path                       	= Str::slug($fileFullName).".".$fileExentsion;
	            $file->move(public_path('profile-pictures/'), $path);
	            $fullpath                   	= 'profile-pictures/'.$path;
	            $user_profile->profile_picture 	= $fullpath;
	            $user_profile->save();
	            return $fullpath;
	        }
        } catch (Exception $ex) {
            error_logs("An error occurred while uploading profile picture!");
        }
    }
Snapey's avatar

So, in $user_profile->profile_picture you save the path to the user's picture as

profile-pictures/timestamp.originalname.originalextension

and the image is in the public_path (/public/profile-pictures etc etc)

so in your controller you need to return the URL to this

$url = asset($user->profile_picture);
1 like
Spiral's avatar

@snapey bro this not working for me

$user = User::find($user->id);
    	$url = asset($user->profile_picture);   
\Log::info($url)                      ===> get ``http://127.0.0.1:8000/  `` 		      	    	  
        $msg = "Signing In.";
        $this->success($msg);
        $this->with('user', $user);
        $this->with('token', $token);
Snapey's avatar

Do you have the URL stored in the database for this user?

have you set APP_URL in config or .env ?

Spiral's avatar

Yes @snapey i have the image path in database but problem is that frontend not now the where it is hosted

and in my .env APP_URL is http://localhost

Snapey's avatar
Snapey
Best Answer
Level 122

open tinker

>>> $u = App\User::find(1)     //insert valid user number

>>> $u->profile_picture;

>>> asset($u->profile_picture);

what do you get

Spiral's avatar

@snapey brother how can return this to api with response

this is my code give me some more help of you so i solve problem

$user = User::find($user->id); 
    	\Log::info( asset($user->profile_picture)); 	
	        	    	  
        $msg = "Signing In.";
        $this->success($msg);
        $this->with('user', $user);
Snapey's avatar

why are you reloading user?

$user = User::find($user->id); 

So you have shown in tinker that the code I suggested works. Just return url with your data

Spiral's avatar

so How can return user with this url where can i put asset functionality

https://imgur.com/hBtmGUO

$user = User::find($user->id); 
    	asset($user->profile_picture); 	
	        	    	  
        $msg = "Signing In.";
        $this->success($msg);
        $this->with('user', $user);

should i store with complete url in database because when user upate profile then what will functionality

Snapey's avatar

You want it as part of the user data, and not returned to the client as a separate property?

first, stop doing this... its pointless.

    $user = User::find($user->id); 

In your user model, add the following;

Public function getPictureAttribute()
{
	return asset($this->profile_picture);
}

add an property called $appends

protected $appends = ['picture'];

https://laravel.com/docs/7.x/eloquent-mutators#defining-an-accessor

https://laravel.com/docs/7.x/eloquent-serialization#appending-values-to-json

1 like
Spiral's avatar

this working fine store database and fetch this is true?

$user_profile->profile_picture 	= asset($fullpath);
$user_profile->save();
Snapey's avatar

Not a good idea since you will then encode the website URL into the profile image.

Just do it as suggested

Or do what you like, I'm done here.

Spiral's avatar

@snapey Thanks dear brother for helping me

why i need to encode i have functionality when user will update picture then old picture delete from database and new picture will upload

private function deleteOldFile($request)
     {
        //check whether there is a new image in the coming requets
        $file = $request->file('profile_picture');
        if(!empty($file) && $file != null)
        {
            //remove image from user table
            $user = User::find(getApiUser()->id);
            if($user){
                $path = $user->profile_picture;

                $user->update([
                    'profile_picture' => ''
                ]);
                
                $fullPath = public_path()."/".$path;
                if(is_file($fullPath))
                {
                    unlink($fullPath);
                }
                
                $fullPath = public_path()."/".$path;
                // $fullPath = str_replace('profile-pictures/', 'profile-pictures/', $fullPath);
                
                if(is_file($fullPath))
                {
                    unlink($fullPath);
                }
            }
            return true;
         } else {
            return false;
         }
     }
Snapey's avatar

No idea what you are talking about, sorry. Was that a question?

Spiral's avatar

@snapey sorry for disturb you... i have not concept so discuss with you..

Thanks alot for lot of helping..

Please or to participate in this conversation.