Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. Schema::create('friend_user', function(Blueprint $table) {
  2. $table->increments('id');
  3. $table->integer('user_id')->unsigned();
  4. $table->integer('friend_id')->unsigned();
  5. $table->timestamps();
  6. });
  7.  
  8. $friend_user = array(
  9. array(
  10. 'id' => 1,
  11. 'user_id' => 1,
  12. 'friend_id' => 3,
  13. ),
  14. array(
  15. 'id' => 2,
  16. 'user_id' => 4,
  17. 'friend_id' => 1,
  18. ),
  19.  
  20. );
  21.  
  22. public function friends()
  23. {
  24. return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
  25. }
  26.  
  27. foreach(Auth::user()->friends as $i) {
  28. var_dump($i->id);
  29. }
  30.  
  31. public function friends()
  32. {
  33. return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
  34. }
  35.  
  36. // Same table, self referencing, but change the key order
  37. public function theFriends()
  38. {
  39. return $this->belongsToMany('User', 'friend_user', 'friend_id', 'user_id');
  40. }
  41.  
  42. //You can then call opposite record(s) using:
  43. foreach( Auth::user()->theFriends as $theFriends )
  44.  
  45. public function friends()
  46. {
  47. return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id')->orWhere('friend_id', $this->id);;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement