【问题标题】:how to view images that stored as array in laravel?如何查看在laravel中存储为数组的图像?
【发布时间】:2018-06-28 10:42:06
【问题描述】:

用户上传多张图片并以数组形式存储。如何在 laravel 刀片文件中显示这些图像?
当我尝试时,我得到了这个错误。

htmlspecialchars() expects parameter 1 to be string, array given (View: C:\Users\user\laravel2\newfyp\resources\views\postqs\show.blade.php)

显示刀片:

   <img src="/image/{{ $postqs['image'] }}" 
    style="width:150px; height:150px;"> </td>

型号:

  protected $casts = [
    'image'=>'array', ];

 protected $fillable = ['image'];

创建刀片

                <div class="form-group">
                    <label class="control-label col-sm-2" >image test:
             </label>
                    <form action="upload" id="upload" enctype="multipart/form-data">
                    <input type="file" name="image[]" id="image" multiple><br />

                </form>
                <div id="message"></div>

【问题讨论】:

  • 你能显示转储的数组代码吗?我的意思是转储数组并在此处显示结果
  • $postqs['image'] 是数组而不是字符串

标签: php arrays database image laravel


【解决方案1】:
protected $fillable = [''image'];

这是双引号的错字吗?

如果图像在数组中,只需遍历数组并为每个图像创建一个 img。

您是否在视图文件中尝试过类似以下内容:

@foreach ($postqs['image'] as $image)
    <img src="asset({{ $image }})" />
@endforeach

【讨论】:

  • 你是如何循环数组的?如果阵列中每个图像的路径都是正确的,则应该没有问题。我使用 asset 助手链接到公共资源,例如:&lt;img src={{ asset('/images/someimage.png') }} /&gt;
【解决方案2】:

首先你应该更正这一行:

protected $fillable = [''image']; // ['image'] and not [''image']

htmlspecialchars(): 当变量为空时,我总是得到这个错误。

一个简单的解决方案是将您的 html 包装为:

@if($yourVar)
@endif

您还可以转储变量以显示结构。

祝你好运。

【讨论】:

    【解决方案3】:

    试试:

    @foreach($postqs['image'] as $imagePath)
        <img src="/image/{{ $imagePath }}" 
            style="width:150px; height:150px;"> 
    @endforeach
    </td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2019-08-08
      • 2016-12-18
      相关资源
      最近更新 更多