1

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>
6
  • Can you share the image link you are getting ? Right click on the broken image icon and click copy image address and paste it here please Commented Jun 6, 2021 at 19:19
  • Your current src is relative src="storage..., add a slash before storage, or try with the asset helper <img src="{{ asset('storage'.$products->image_path) }}">. Commented Jun 6, 2021 at 19:46
  • here is the link (127.0.0.1:8000/singlepost/storage/…) @matiaslauriti Commented Jun 6, 2021 at 19:50
  • @porloscerrosΨ i tried this before but it doesn't worked Commented Jun 6, 2021 at 19:50
  • Did you run the php artisan storage:link command? Is the file stored in your-project/storage/app/public directory? Commented Jun 6, 2021 at 19:55

1 Answer 1

1

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 ..." />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.