Laravel Many to Many get collection of third instance selection

patrick

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
har2vey

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

laravel many to many with third table

From Dev

how to get the third properties from many to many relationship tables?

From Dev

How to get many to many relationship items in laravel

From Dev

Laravel get data from many to many relation

From Dev

Laravel Many to many relationship get specific data

From Dev

Laravel: get Many-to-Many column data

From Dev

How to sum total price from a Many to Many relationship collection in Laravel

From Dev

Laravel Eloquent how to return one to many to many collection

From Dev

Get total occurrences of each instance in many to many pivot table

From Dev

Many-to-Many mySQL selection

From Dev

Laravel - Many to Many relationship

From Dev

Laravel - Many To Many

From Dev

Laravel Many to Many relation

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

From Dev

Regrouping values from a many relationship in Laravel ? (collection of collection)

From Dev

Test a collection is Many to many or many to one

From Dev

Laravel 4 : How to get selected/specific columns in a many to many relationship?

From Java

get many to many values and which of them are selected in laravel

From Dev

Laravel: Get pivot data for specific many to many relation

From Dev

Laravel: belongsToMany() does not get fields in the many-to-many table

From Dev

Many to Many Get All Id Function Laravel 5.2

From Dev

Laravel - Eloquent relation - many-to-many - get intermediate table columns

From Dev

Hibernate Many to Many without third table

From Dev

how get many record from an array in laravel

From Dev

Laravel Get data One to Many Relationship with conditions

From Dev

Laravel eloquent many to many to many model

From Dev

How to get items from many to many relationships' one to many relationship in Laravel?

Related Related

  1. 1

    laravel many to many with third table

  2. 2

    how to get the third properties from many to many relationship tables?

  3. 3

    How to get many to many relationship items in laravel

  4. 4

    Laravel get data from many to many relation

  5. 5

    Laravel Many to many relationship get specific data

  6. 6

    Laravel: get Many-to-Many column data

  7. 7

    How to sum total price from a Many to Many relationship collection in Laravel

  8. 8

    Laravel Eloquent how to return one to many to many collection

  9. 9

    Get total occurrences of each instance in many to many pivot table

  10. 10

    Many-to-Many mySQL selection

  11. 11

    Laravel - Many to Many relationship

  12. 12

    Laravel - Many To Many

  13. 13

    Laravel Many to Many relation

  14. 14

    Many to many relationship in Laravel

  15. 15

    Many to Many relationship with Laravel

  16. 16

    Many to Many laravel

  17. 17

    Regrouping values from a many relationship in Laravel ? (collection of collection)

  18. 18

    Test a collection is Many to many or many to one

  19. 19

    Laravel 4 : How to get selected/specific columns in a many to many relationship?

  20. 20

    get many to many values and which of them are selected in laravel

  21. 21

    Laravel: Get pivot data for specific many to many relation

  22. 22

    Laravel: belongsToMany() does not get fields in the many-to-many table

  23. 23

    Many to Many Get All Id Function Laravel 5.2

  24. 24

    Laravel - Eloquent relation - many-to-many - get intermediate table columns

  25. 25

    Hibernate Many to Many without third table

  26. 26

    how get many record from an array in laravel

  27. 27

    Laravel Get data One to Many Relationship with conditions

  28. 28

    Laravel eloquent many to many to many model

  29. 29

    How to get items from many to many relationships' one to many relationship in Laravel?

HotTag

Archive