如何在 Octobercms 的前端显示 attachMany 图像?

How to display attachMany images in frontend in Octobercms?

这应该很简单,但无法弄清楚缺少什么。我试图在前端显示 attachMany 但它不起作用。

它在后端工作正常,只有在我将其更改为 attachOne 后才能在前端工作

    `{% for image in ads.gallery %}
   <img src="{{ image.gallery.thumb(100, auto) }}">
  {% endfor %}`

我希望它能显示在前端图库中上传的所有图片

您只需从下面的代码中删除 gallery 即可。

<img src="{{ image.thumb(100, auto) }}">

在关系 attachOne 中,它正在工作,因为即使在通过 {{ image.gallery.thumb(100, auto) }} 使用 for loop 之后,您仍试图获取图库。

对于 attachMany 关系,你需要做这样的事情。

{% for image in ads.gallery %}
   <img src="{{ image.thumb(100, auto) }}">
{% endfor %}

有关更多信息,请访问此官方文档link Here.

还有,如果还有什么疑问,欢迎评论。

编辑

所以你在页面上使用了这个查询,

$this['ads'] = Advert::whereHas('cats', function ($query) use ($slug) 
              { 
                 $query->where('slug',$slug); 
              })->get();

现在,根据您的查询,您正在检索页面上的多个 ads,因此您需要编写一个 for 循环让您的广告在页面上展示并在图库中循环展示

{% for ad in ads %}
   //to get values from you can do {{ad.name}}
   //to get gallery values from particular ad, you will need to write for loop again(because you have used relation attachMany) 
    {% for image in ad.gallery %}
       <img src="{{ image.thumb(100, auto) }}">
    {% endfor %}
{% endfor %}

要检索图库,您应该使用 eager loading,因此您的查询应该是

$this['ads'] = Advert::with('gallery')->whereHas('cats', function ($query) use ($slug) 
              { 
                 $query->where('slug',$slug); 
              })->get();