【问题标题】:Image not displaying in frontend. Laravel图片未显示在前端。拉拉维尔
【发布时间】:2021-07-05 05:43:12
【问题描述】:

在我的 laravel 项目中,我试图在表格中显示博客文章的标题和图片。帖子的图像被上传并存储在公共/存储中。 这是我用来存储有效图像的代码

$image = $request->image->store('posts');

在 index.blade.php 中我使用此代码来显示帖子

图片 标题
        <tbody>
        
           @foreach ($posts as $post )

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

            <td>
                {{$post->title}}
            </td>
           </tr>
               
           @endforeach

        </tbody>
    
    </table>

前端图像出现损坏,我似乎无法修复This is how it shows 我已成功执行php artisan storage:link,但仍然没有结果。 我已经在 .env 文件中添加了这行代码FILESYSTEM_DRIVER=public

如果这很重要,这就是我的 filesystems.php 文件的外观

<?php

返回 [

/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/

'default' => env('FILESYSTEM_DRIVER', 'local'),

/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/

'cloud' => env('FILESYSTEM_CLOUD', 's3'),

/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],


    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],

],

/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/

'links' => [
    public_path('storage') => storage_path('app/public'),
    
],

];

有谁知道为什么会这样,我的代码中是否有我看不到的错误?

【问题讨论】:

  • 您尝试过什么解决问题的方法?检查生成的标记是否存在问题应该不会太难

标签: php laravel


【解决方案1】:

您可以使用\Storage 外观获取图像。它将在 public 文件夹中创建一个指向您的 storage 文件夹的链接。

<img src="{{ \Storage::disk('public')->url($post->image) }}" width="120px" height="60px" alt="">

【讨论】:

    【解决方案2】:

    试试这个,

     <img src="{{ Storage::url($post->image) }}" width="120px" height="60px" alt="">
    

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 2015-03-28
      • 2015-04-29
      • 2018-11-12
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多