How to Show Image from Storage Folder in Laravel?

Jul 01, 2024 | Laravel 11 Laravel


Hello Dev,

Developers and clients often aim to securely store images or files on their servers. so anyone can not access directly using url. that's the reason laravel introduce storage folder file upload. Almost using storage folder for uploading files and images on his laravel app. However, once uploaded, there may be a need to display or download these files.

However, In this example, i will give you 2 way to show your image in blade file.

Method: 1

first of all ,To display an image using the `asset` function, you first need to create a symbolic link to your storage folder.

1. Open your terminal and navigate to your Laravel project.

2. Run the following Artisan command:

php artisan storage:link

After creating the symbolic link from `public/storage` to `storage/app/public`, files stored in the `storage` folder can be accessed using the `public/storage` URL.

Note: show this type error “error: link already exists”; no worries, it means it’s set up correctly!

Add your image in blade:

<img src="{{ asset(.'/images/'.$article->image) }}" alt="" title="">
You can use any of the methods in your code.
<img src="{{ url('storage/images/your-image-name.jpg') }}" alt="" title="">
Method: 2 Showing an Image From Storage Using Route and Controller
php artisan make:controller ImageController
app/Http/Controllers/ImageController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;

class ImageController extends Controller
{
    /**
     * Display the specified image.
     *
     * @param  string  $imageName
     * @return \Illuminate\Http\Response
     */
    public function show($imageName)
    {
        // Construct the full path to the image within the storage directory
        $path = storage_path("app/public/images/{$imageName}");

        // Check if the image exists; if not, return a 404 response
        if (!Storage::exists("public/images/{$imageName}")) {
            abort(404);
        }

        // Return the image as a response
        return response()->file($path);
    }
}
routes/web.php
<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

// Add a route that can load an image from storage based on a parameter
Route::get('/images/{imageName}', [ImageController::class, 'show'])->name('image.show');
In your Blade file, use the following to display the image:
<img src="{{ route('image.show', ['imageName' => $video->image]) }}" alt="{{ $video->image }}">

Feel free to ask if you have any questions or need further clarification!



Tags :
#Laravel 11
#Laravel
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"