duanguan3863 2017-03-28 16:07
浏览 10

在laravel中展示产品的每个单一图像

I have double tables one for products and one for files, files table have multi images related to the one product.

I want to show only one image for each product not all of them.

I make this code but it's back with the for example 3 times with the first image!

@foreach($lastProducts as $key => $lastProduct)
@if($key == 1)
<div class="bl-thumb">
<img src="{{url('website/'.$lastProduct->path.$lastProduct->->file}}">
</div>
@endif
@endforeach

I need to show the first image at my page and other pages show the second and third image for the product.

And here's my view which want to show all images on it :

<div class="slim-scroll">
    <div class="item active">
        <a href="" title="" data-lg-img="/website/images/{{$product->image_1}}" class="pic"><img src="/website/images/{{$product->image_1}}" alt="" class="img-responsive" /></a>
    </div>
    <div class="item">
        <a href="javascript:void(0)" title="" data-lg-img="/website/images/{{$product->image_2}}" class="pic"><img src="/website/images/{{$product->image_2}}" alt="" class="img-responsive" /></a>
    </div>
    <div class="item">
        <a href="javascript:void(0)" title="" data-lg-img="/website/images/{{$product->image_3}}" class="pic"><img src="/website/images/{{$product->image_3}}" alt="" class="img-responsive" /></a>
    </div>
    <div class="item">
        <a href="javascript:void(0)" title="" data-lg-img="/website/images/{{$product->image_4}}" class="pic"><img src="/website/images/{{$product->image_4}}" alt="" class="img-responsive" /></a>
    </div>
</div>

It's showing me something like that without the foreach

enter image description here enter image description here

  • 写回答

1条回答 默认 最新

  • duanboniao5903 2017-03-28 17:29
    关注

    If I understand correctly, you have multiple products and each product has multiple images. If on page 1 you want to display product 1 image 1, product 2 image 1, product 3 image 1...then on page 2 you want to display product 1 image 2, product 2 image 2, product 3 image 2 you could try something like this:

    Page 1:

    @foreach($products as $product)
       <div class="bl-thumb">
          <img src="{{url('website/' . $product->images->pull('1')->path . $product->images->pull('1')->file}}">
       </div>
    @endforeach
    

    Page 2:

    @foreach($products as $product)
       <div class="bl-thumb">
          <img src="{{url('website/' . $product->images->pull('2')->path . $product->images->pull('2')->file}}">
       </div>
    @endforeach
    

    Really this logic should be in the controller though. You could use a get variable to define what page you are on and filter for the proper image before you render the page.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题