简体   繁体   English

尝试显示数据库中的图像时出现问题

[英]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).我试图通过id从我的数据库中显示一行,数据显示成功但图像不显示(显示断开的链接图标)。

Here is the index.blade.php where the href to the row:这是index.blade.php其中行的href

<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 function getPostById

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

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

The singlepage.blade.phpsinglepage.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>

When referencing publically accessible files, you want to do so from the /public/storage directory.引用可公开访问的文件时,您希望从/public/storage目录执行此操作。 Files from the /storage/app/public directory should be mapped from this directory to /public/storage using a symbloic link, /storage/app/public目录中的文件应使用符号链接从该目录映射到/public/storage

You create the symlink using:您使用以下命令创建symlink

php artisan storage:link

With that done, assets in /storage/app/public can now be accessed as if they existed in /public/storage .完成后,现在可以访问/storage/app/public中的资产,就好像它们存在于/public/storage中一样。 Use the asset helper to get your image.使用asset助手来获取您的图像。

<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.以上假设资产的相对路径来自/public/storage根目录,如果您在子目录中有图像,您也需要包含它。

For example, if you had your images in an img subdirectoy:例如,如果您将图像放在img子目录中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM