How to show response path when update image in laravel rest api?

I'm trying to build a REST API with Laravel where users need to update their images. In this case the image has been successfully saved in storage, but I want a response in the form of a link that can be accessed by the frontend later. However, the response was not found. Is there a solution to this problem? Here I attach my code

 public function update(Request $request,$userId)
{
    $user= User::find($userId->id);

    // $photoWithExt= $request->file('photo')->getClientOriginalName();
    $filename = $user['nip'];
    $extension = $request->file('photo')->getClientOriginalExtension();
    $fileNameToStore ='/images/users/'.$filename.'.'.$extension;
    $path= $request->file('photo')->storeAs('',$fileNameToStore);

    $user->update([
        'username'=>$request['username'],
        'name'=>$request['name'],
        'photo'=> $path
    ]);

    return $user;
}

This is response in postman enter image description here

And when I click the link path, the image is 404. I hope someone can help with this problem


Solution 1:

Assuming that you're using Local Drive, you have to get the absolute link to the file

  (...)

  $user->update([
      'username'=>$request['username'],
      'name'=>$request['name'],
      'photo'=> Storage::disk('local')->get($path); // <---
  ]);

  return $user;
}

Solution 2:


I have found the answer,

public function update(Request $request,$userId)
{
    $user= User::find($userId->id);
    $filename = $user['nip'];
    $extension = $request->file('photo')->getClientOriginalExtension();
    $fileNameToStore ='images/users/'.$filename.'.'.$extension;
    $path= $request->file('photo')->storeAs('',$fileNameToStore,'public');
    $photoURL = Storage::url($path);       //base_url
    $user->update([
        'username'=>$request['username'],
        'name'=>$request['name'],
        'photo'=> $photoURL,
    ]);

    return $user;
}