2013-07-25 8 views
8

雄弁と友情関係をモデル化する最良の方法は何ですか?私のテーブルのスキーマは以下の通りです。私はすべての友人を以下のように取得できる1つの関係を定義したいと思います。Laravel ORM重複のない友情関係

<?php 

class User extends Eloquent { 

public function friends() { 
    return $this->belongsToMany('User', 'friendships', 'user_id', 'friend_id')->orWhere($this->id,'=', 'friend_id'); 
    } 
} 

+----+---------+-----------+----------+---------------------+---------------------+ 
| id | user_id | friend_id | state | created_at   | updated_at   | 
+----+---------+-----------+----------+---------------------+---------------------+ 
| 1 |  3 |   1 | accepted | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 
| 2 |  2 |   3 | accepted | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 
+----+---------+-----------+----------+---------------------+---------------------+ 

上記の関係は、私は3のユーザIDと友達を探したときに、私は、ユーザー1と3を得ることが、明らかに私は、1と2

友情表

をしたい作業に近いです

user_id:友だちを要求したユーザID
friend_id:対象となる友人のユーザID
state:友情が保留中か、受け入れられているか、ブロックされているかどうか。
のcreated_atと

updated_atの私は関係の両側からの友人を取得することができますが、ユーザー1と3が友達であるならば、私はそれから1で、例えば2列でなければなりませんLaravel Many to many self referencing table only works one wayから解決策があることを承知していますrow_user_id = 3、friend_id = 1であり、次の行はその逆である。 (2つの行がない場合は、2つのクエリを実行する必要があります)。

答えて

3
あなたがしようと、あなたは間違いないことをやろうとしている場合は、試してみて、1行に2行がどうあるべきか回し、しかしべきではありません

二回データベースをヒットする必要があります。

select * from users where (user_id = :user_id and friend_id = :friend_id) or (friend_id = :friend_id and user_id = :user_id) 

Laravelであろうこと:

Users::whereRaw('(user_id = ? and friend_id = ?) or (friend_id = ? and user_id = ?)', [    
    $user_id, 
    $friend_id, 
    $friend_id, 
    $user_id 
]); 

あなたがグループトンにサブのwheresをも行うことができます裾、それは少し複雑です。

5

and use a union queryという2つの検索を実行することができます。したがって、データベースに1回しか到達しません。カスタム関数にこのすべてを置く:

class User extends Eloquent { 

    public function friends() 
    { 
    $first = $this->belongsToMany('User', 'friendships', 'user_id', 'friend_id'); 
    return $this->belongsToMany('User', 'friendships', 'friend_id', 'user_id')->union($first); 
    } 
} 
+0

ありがとうございますが、このエラーが発生しました。引数1がIlluminate \ Database \ Query \ Builder :: mergeBindings()に渡されている必要があります。Illuminate \ Database \ Query \ Builderのインスタンス、Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany与えられた/var/www/cc/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php 857行で呼び出され、 –

-3

を私はあなたがこの例では

  • をしたいvlauesをロードするための条件を使用しての提案を持っているのは、あなたがuser_idをロードするためのクエリを使用してfiend_idとしましょう条件で、

    "frienddship SELECT * FROM WHERE user_IDは= '$ IDを' OR friend_ID = '$のID'、"

$ id:あなたの友達を表示するユーザーIDです。 uはあなたがボーテ表の列からデータをロードし、その後、各時間が列をフィルタリングこの場合条件

while ... { 

if (friendship['user_id'] == $id) { 

$f_id = friendship['friend_id'] ; } 

// other instructionS... 

else { 

$f_id = friendship['user_id'] ; } 

// other instructionS... 

} 

を行うことでresuletsをフィルタリングすることができ、結果をロードするために使用するPHPでWHILEループ内

ユーザーIDで、自分の友人のIDだけを聞かせて、自分の友人であることをユーザーに知らせないフィルターを使用していました。

は申し訳ありません、私は例を説明するためにMySQLを使用

+0

が定義されています雄弁ですか? –

関連する問題