2016-07-30 2 views
1

ユーザーがフレンドリクエストを送信し、別のユーザーがそれを受け入れると、フレンドリストが両方のユーザーに対して変更されるようにする必要があります。要求を受け入れた人だけでなく、これが問題です。ユーザーがフレンドリクエストを受け入れると、フレンドはフレンドリストに表示されますが、フレンドリクエストを送信したユーザーは表示されません。Laravel。両方のユーザーの受け入れた友人を表示する

これは私のコントローラ機能である:

public function add($id) // Add friends 
    { 
     $friend = new Friend(); 
     $friendId = User::where('id', $id)->first(); 
     $friend->user_id = Auth::user()->id; 
     $friend->friend_id = $friendId->id; 
     $friend->save(); 
     return redirect('/'); 
    } 

は友人が

public function showRequests() 
{ 
     $requests = Friend::where('friend_id', '=', Auth::user()->id)->where('accepted', '=', 0)->get(); 
     return view('users.friends.requests', compact('requests')); 
} 

public function accept($id) 
{ 
    $friend = Friend::where('id', $id)->first(); 
    $friend->accepted = 1; 
    $friend->save(); 
    return redirect()->back(); 
} 

ショーが

public function show() 
{ 
    $friends = Friend::where('friend_id', '=', Auth::user()->id)->where('accepted', '=', '1')->get(); 
    return view('users.friends.show', compact('friends')); 
} 
をfriendlist友達リクエストを受け入れを要求表示

関係。 User.php:

public function friend() 
{ 
    return $this->hasMany('App\Friend'); 
} 

Friend.php:

public function user() 
{ 
    return $this->belongsTo('App\User'); 
} 
+0

私はおそらくこれらのフィールドを持つデータベースにそのhttps://github.com/laravel/framework/issues/441 – ExoticChimp

答えて

1

あなたがpivot tableを追加することを検討していますか? Laracasts/Generatorsを使用すると簡単にピボットを行うことができます。

あなたは、ピボットテーブルを作成したら、あなたは

その後
public function add(...) { // Add friends 
    $friend1 = User::find(1); 
    $friend2 = User::find(2); 
    $friend1->friends()->attach($friend2); 
} 

のようなものが関係を得るためにやる、あなたがやる:

public function show($id) { 
    $user = User::find($id) 
    return $user->friends()->get() 
} 

そして、あなたの受け入れ/保留/拒否された要求をピボットテーブルの単純な列にすることができます。

またUserモデルのためfriends()機能を実装する必要があります:

class User extends Authenticatable { 
... 
    public function friends() { 
     return $this->belongsToMany('App\User')->withTimestamps(); 
    } 
} 
+0

友人テーブルに行くと思いますので、テイラーは実際にGitHubの問題で、この具体例に言及:「USER_ID "、" friend_id "、" accepted "で十分ではありませんか? –

+0

@labasGamePagevisogeroしかし、1人の「User」が複数の他のユーザーと友人である場合はどうなりますか? 'friend_id'カラムにすべてのidをどのように保存しますか?たぶん私は間違っていますが、私は友人と友人関係を双方向の関係と考えています。そのため、友人は 'friend_id'を維持しなければなりません。どうすればそれを知ることができますか。後で検索と並べ替えを行います。 – Grigio

+0

だから私は "友達"テーブルを削除し、 "friend_user"のような新しいテーブルを作成する必要がありますか? –

-1

は、あなたはこれを試すことができますか?

public function show() 
{ 
    $friends = Auth::user()->friend()->where('accepted', '=', '1')->get(); 
    dd($friends); 
} 
+0

あなたが書いているコードの説明を常に提供してください。コードのみの回答は、将来の読者にとってあまり有用ではありません – ExoticChimp

1

これはfriendsテーブルでのみ機能しました。

public function friendsOfMine() 
{ 
    return $this->belongsToMany('App\User', 'friends', 'user_id', 'friend_id'); 
} 

public function friendOf() 
{ 
    return $this->belongsToMany('App\User', 'friends', 'friend_id', 'user_id'); 
} 

public function friends() 
{ 
    return $this->friendsOfMine()->wherePivot('accepted', true)->get()->merge($this->friendOf()->wherePivot('accepted', true)->get()); 
} 
関連する問題