Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Schema::create('friend_user', function(Blueprint $table) {
- $table->increments('id');
- $table->integer('user_id')->unsigned();
- $table->integer('friend_id')->unsigned();
- $table->timestamps();
- });
- $friend_user = array(
- array(
- 'id' => 1,
- 'user_id' => 1,
- 'friend_id' => 3,
- ),
- array(
- 'id' => 2,
- 'user_id' => 4,
- 'friend_id' => 1,
- ),
- );
- public function friends()
- {
- return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
- }
- foreach(Auth::user()->friends as $i) {
- var_dump($i->id);
- }
- public function friends()
- {
- return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id');
- }
- // Same table, self referencing, but change the key order
- public function theFriends()
- {
- return $this->belongsToMany('User', 'friend_user', 'friend_id', 'user_id');
- }
- //You can then call opposite record(s) using:
- foreach( Auth::user()->theFriends as $theFriends )
- public function friends()
- {
- return $this->belongsToMany('User', 'friend_user', 'user_id', 'friend_id')->orWhere('friend_id', $this->id);;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement