在关系 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();