【问题标题】:How to display attachMany images in frontend in Octobercms?如何在Octobercms的前端显示attachMany图像?
【发布时间】:2019-03-26 22:31:52
【问题描述】:

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

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

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

我希望它能显示所有上传到前端画廊的图片

【问题讨论】:

    标签: laravel twig octobercms


    【解决方案1】:

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

    对于 attachMany 关系,您需要执行类似的操作。

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

    欲了解更多信息,请访问此官方文档链接Here.

    如果您仍有任何疑问,请发表评论。

    编辑

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

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

    现在,根据您的查询,您正在网页上检索多个广告,因此您需要编写一个 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();
    

    【讨论】:

    • 有什么我想添加到这个的,因为那是我从中提取它的地方。 $this['ads'] = Advert::whereHas('cats', function ($query) use ($slug) { $query-&gt;where('slug',$slug); })-&gt;get(); 还没有运气
    • @OsujiKingsley 我明白了你的困惑。我已经编辑了我的答案。请理解并尝试解决您的问题。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多