【问题标题】:Laravel: stored image from the database doesn't show upLaravel:来自数据库的存储图像不显示
【发布时间】:2020-02-27 05:25:43
【问题描述】:

我正在使用 Laravel,在显示数据库中存储的图像时,图像没有显示出来:

$post -> image has posts/1lSBv7Ana8Lonj9Au37IrujjnnEaYgzNWyamch33.jpeg as image location, still image doesn't show up.

index.blade.php

        <tbody>
            @foreach ($posts as $post)
            <tr>
                <td>
                    <img src="{{$post->image}}" width="60px" height="60px" alt="image">
                </td>
                <td>{{$post->title}} </td>
            </tr>
            @endforeach
        </tbody>

和 PostsController 一样

public function index()
{
    return \view('posts.index')->with('posts', Post::all());
}

【问题讨论】:

  • 使用 src="{{asset($post->image)}}"
  • 给出存储图片的文件夹路径

标签: php html laravel ubuntu content-management-system


【解决方案1】:

如果您使用的是本地或公共磁盘存储,则可以使用

<img src="{{ asset('storage/' . $post->image) }}">

【讨论】:

    【解决方案2】:

    发出命令

    php artisan storage:link
    

    然后用asset()函数包装你的图片名称

    <img src="{{asset($post->image)}}" width="60px" height="60px" alt="image">
    

    【讨论】:

      【解决方案3】:

      首先,您必须将图像存储在公共目录中(否则浏览器无法访问它们)

      那么这取决于 $post->image 实际上是什么。如果它是相对于公共的路径,您可以这样做:

      <img src="{{ asset($post->image) }}" />
      

      或者这个

      {{ HTML::image($post->image, '', array('class' => 'image')); }}
      

      【讨论】:

      • 你必须在前面加上:&lt;img src="{{ asset('img/' . $post-&gt;image) }}" /&gt;
      【解决方案4】:

      根据 Laravel 文档 你可以使用asset()辅助函数来获取图片

      asset()asset 函数使用 请求的当前方案(HTTP 或 HTTPS)

      <img src="asset({{$post->image}})" width="60px" height="60px" alt="image">
      

      参考:

      Laravel -> Helpers -> Method asset

      【讨论】:

        【解决方案5】:

        很抱歉给你添麻烦了,但我已经摆脱了它,

        php artisan storage:link
        

        在索引页中

        <img src="{{asset("storage/$post->image")}}" width="60px" height="60px" alt="image">
        

        【讨论】:

          猜你喜欢
          • 2017-06-18
          • 2021-12-01
          • 2020-03-03
          • 1970-01-01
          • 1970-01-01
          • 2020-05-19
          • 1970-01-01
          • 1970-01-01
          • 2020-10-18
          相关资源
          最近更新 更多