Laravel 5 - access specific model on many to many relationship

Greg

So I am learning Laravel 5 and have a question regarding many to many relationships. I'm building a twitter clone, and want to allow for creating tweets and retweeting. My database setup is (as outlined in the laravel docs for a pivot table)

users
  - id
  - name

tweets
  - id
  - content

tweet_user
  - tweet_id
  - user_id

My User and Tweet model have the appropriate belongsToMany calls to hook up those relationships.

from User.php

public function tweets() {
    return $this->belongsToMany('App\Tweet');
}

from Tweet.php

public function users() {
    return $this->belongsToMany('App\User');
}

Finally, my tweet_user pivot table looks like:

|--------------------|
| tweet_id | user_id |
|--------------------|
| 1        | 1       |
|--------------------|
| 1        | 2       |
|--------------------|

So to display tweets I do:

return view('tweets.index')->withTweets(App\Tweet::all())

And pass that to my view. All good at this point, my view will display 2 tweets (as seen in the tweet_user pivot table above) My view then has:

@foreach($tweets as $tweet)
   {{ $tweet->user->name }} <-issue is here
   {{ $tweet->content }} <- all good here
@endforeach

So since I have this setup as many to many, the $tweet->user is an array / collection of all users that have this tweet. How do I access the actual user model for the current tweet in the loop above (IE user id 1 for the first loop, user id 2 for the second)? I'm sure I'm missing something simple, but do I need some other accessor on the tweet / user model? I initially had this setup as a hasMany / belongsTo relationship, so the loop above worked fine. Thanks for any help!

Chris

I would rename $this->user to $this->users if it were a many to many, just for the ease of reading.

Then something like:

@foreach($tweets as $tweet)
   @foreach($tweet->users as $user)
      {{ $user->name }}
   @endforeach
   {{ $tweet->content }} <- all good here
@endforeach

This is because you have a many to many relationship set up. You might want to sort by order or someway to flag the original user though.

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 4 : How to get selected/specific columns in a many to many relationship?

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel - Many to Many relationship

From Dev

Laravel 5.1 Multiple Many to Many Relationship on the Same Model

From Dev

Laravel 5 Deleting a one-to-many relationship

From Dev

Many to many relationship in laravel need a specific name for the table?

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

Laravel many-to-many relationship on the same model

From Dev

Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

From Dev

Laravel 5.2 Find a Model Based on a Many-to-Many relationship

From Dev

Building a dynamic query of a many-to-many relationship in Laravel 5

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel 6 many to many relationship

From Dev

This is a many to many relationship model for question

From Dev

Laravel Many to many relationship get specific data

From Dev

Pagination with many to many relationship in laravel

From Dev

Specific result in many to many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Laravel many to many relationship error

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Laravel many to many relationship with conditions

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Laravel ORM many to many relationship

From Dev

Laravel one to many model relationship not working

From Dev

User to User Model: One to Many Relationship on Laravel

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

Related Related

  1. 1

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

  2. 2

    Laravel 5 Eloquent Relationship - Has Many Though

  3. 3

    Laravel - Many to Many relationship

  4. 4

    Laravel 5.1 Multiple Many to Many Relationship on the Same Model

  5. 5

    Laravel 5 Deleting a one-to-many relationship

  6. 6

    Many to many relationship in laravel need a specific name for the table?

  7. 7

    Empty data returning from many to many relationship laravel 5

  8. 8

    Laravel many-to-many relationship on the same model

  9. 9

    Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

  10. 10

    Laravel 5.2 Find a Model Based on a Many-to-Many relationship

  11. 11

    Building a dynamic query of a many-to-many relationship in Laravel 5

  12. 12

    Laravel 5 Eloquent count many to many relationship

  13. 13

    Laravel 6 many to many relationship

  14. 14

    This is a many to many relationship model for question

  15. 15

    Laravel Many to many relationship get specific data

  16. 16

    Pagination with many to many relationship in laravel

  17. 17

    Specific result in many to many relationship

  18. 18

    Laravel 5 one to many eloquent relationship

  19. 19

    Laravel 5 Eloquent Relationship - Has Many Though

  20. 20

    Laravel 5 | Many to Many Relationship not working

  21. 21

    Laravel many to many relationship error

  22. 22

    Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

  23. 23

    Laravel many to many relationship with conditions

  24. 24

    Many to many relationship in Laravel

  25. 25

    Many to Many relationship with Laravel

  26. 26

    Laravel ORM many to many relationship

  27. 27

    Laravel one to many model relationship not working

  28. 28

    User to User Model: One to Many Relationship on Laravel

  29. 29

    Laravel L5.5 No access to pivot table in "Many to many" relationship

HotTag

Archive