Gazdallah Amira
Gazdallah Amira

Reputation: 220

problem while trying to display image from database

I am trying to display a row from my database by id, the data displayed successfully but the image doesn't show (showing a broken link icon).

Here is the index.blade.php where the href to the row:

<a href="singlepost/{{$products->id}}">
<img src="storage/{{$templates->image_path2}}">

The route:

Route::get('/singlepost/{id}', 'App\http\controllers\TemplatesController@getPostById')->name('single.post');

The function getPostById

public function getPostById($id){
    $products = DB::table('templates')->where('id', $id)->first();

    return view('singlepost', compact('products'));
}

The singlepage.blade.php

<div class="row">
    <div class="col-lg-4">
        <img src="storage/{{$products->image_path}}">
    </div>
    <div class="col-lg-7">
        <h2>{{$products->name}}</h2>
        <p>{{$products->description}}</p>
    </div>
</div>

Upvotes: 1

Views: 100

Answers (1)

Peppermintology
Peppermintology

Reputation: 10210

When referencing publically accessible files, you want to do so from the /public/storage directory. Files from the /storage/app/public directory should be mapped from this directory to /public/storage using a symbloic link,

You create the symlink using:

php artisan storage:link

With that done, assets in /storage/app/public can now be accessed as if they existed in /public/storage. Use the asset helper to get your image.

<img src="{{ asset($templates->image_path2) }}" alt="Some alt tag ..." />

The above assumes a relative path of the asset from the /public/storage root, if you had the image in a subdirectory you would need to include that too.

For example, if you had your images in an img subdirectoy:

<img src="{{ asset('/img/' . $templates->image_path2) }}" alt="Some alt tag ..." />

Upvotes: 1

Related Questions