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

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
    }
}

Try this:

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

$categorie_photos = array();

foreach ($all_related_hashtags as $hashtag) {

   $current_photos = array();

   foreach ( $hashtag->photos as $photo ) {
      $current_photos[] = $photo;
   }

   array_merge($categorie_photos, $current_photos);

}