dtzh131555 2014-01-21 05:28
浏览 128
已采纳

Laravel Many to Many获得第三个实例选择的集合

I am trying to grab collections form many to many relationships in my application.

To explain very quickly: Lets say I have categories that themselves have dedicated hashtags. Those hashtags again have photos.

Now, I want to grab one category and its dedicated hashtags. But I also want to grab all photos that are associated with those hashtags.

So here is what I have tried but still, this does not work:

 $all_related_hashtags = $categorie->hashtags()->get();

 $photos = foreach ($all_related_hashtags as $hashtag) {

        $hashtag->photos;

      }

Is there any way to fix this?

Any help would be awesome.

Thanks.

EDIT

@foreach ($all_related_hashtags as $hashtag)

@foreach(array_chunk($hashtag->photos, 4) as $row)

        <div class="row">

                @foreach($row as $photo)

SECOND EDIT

@foreach ($all_related_hashtags as $hashtag)

@foreach ($hashtag->photos as $photo)

    {{ $photo->title }}

@endforeach

@endforeach
  • 写回答

2条回答 默认 最新

  • duandang9434 2014-01-21 07:02
    关注

    Try this instead

    $all_related_hashtags = $categorie->hashtags()->get();
    
    $photos = array();
    
    foreach ($all_related_hashtags as $hashtag) {
    
        $photos[] = $hashtag->photos;
    
    }
    

    If you just want to display in your view the photos for each hashtag you can actually call the photos in your view using nested loop such as:

    foreach ($all_related_hashtags as $hashtag) {
        foreach ($hashtag->photos->take($limit) as $photo){
            echo $photo->url
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码