Search code examples
laravellaravel-storage

How to display the user image from storage/app?


How can I display images from the storage folder (outside public)? I stored my image files in storage/usernamee, but I'm unable to show it in the view. How can I move the upload into "storage/images/" (under a folder name "images")?

View

<form class="forms-sample" method="post" action="/updateprofil" enctype="multipart/form-data">
    @method('put')
    @csrf
    <div align="center">
        <img alt="User Pic" src="{{  storage_path($membre->photo) }}" id="profile-image1"
             class="img-circle img-responsive">
        <input id="pic" class="hidden" name="pic" type="file" accept="image/*">
        <div style="color:#999;">click here to change profile image</div>
    </div>
    <input type="hidden" id="idMembre" name="membre_id" value="{{  $membre->id }}">
</form>

Controller

public function updateprofil(Request $request)
{
    $request->validate(['cin' => 'size:8']);

    if ($request->hasFile('pic')) {
        $image = $request->file('pic');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        Image::make($image)->resize(300, 300)->save(storage_path("app/usernamee/" . $filename));
        $request->pic = $filename;
    }

    $membre = Membre::findOrFail($request->membre_id);
    $membre->photo = 'app/usernamee/' . $request->pic;
    $membre->update($request->all());

    return redirect('profil');
}

This is my view of user profile

PS: The update method works for informations update but doesn't work for the photo.


Solution

  • You either or need to:

    1) need to save the image in storage/app/public directory - First you need to symlink the storage path to public:

    php artisan storage:link

    Then your way should work.

    2) Create a route to return the image.

    Route::get('show-image/{id}', function() {
       $membre = Membre::findOrFail($id);
    
       $file = storage_path($membre->photo);
    
       return response()->file($file);
    })