2017-07-12 1 views
0

Iている以下の表 Laravel 5. * - 多くの関係に多くの雄弁

USERS = username | email | name 
FOLLOWERS = user_id | follower_id 

場合はログインしているユーザーがクリック私のコードは、 followers.follower_id内の彼のIDを保存し、ユーザーのIDを「追跡」に誰が彼が従いたいと思っているのは followers.user_idの中に保存されています。

ユーザーが持っていると、ユーザーが私が使用する次の方法を多くのユーザーがどのように多くのフォロワーを参照するには、次の

$followers = Follower::where('user_id', $user->id)->count(); 
$following = Follower::where('follower_id', $user->id)->count(); 

これはうまく動作しますが、私は一人のユーザのフォロワーに関する情報を表示したいと思います。私は以下を試しました:

$first_follower = $followers[0]->user->username; 

しかし、それはフォロワーに従わずにユーザーを返します。

私はこの権利を取得する場合の信者がインスタンスである私は、フォロワーに関する情報を取得することができますどのように

ユーザーモデル

protected $fillable = ['username','email','name']; 

public function follow() { 
    return $this->hasMany('Shop\Follower');  
} 

フォロワーモデル

protected $fillable = ['user_id','follower_id']; 

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

この問題を解決するには、多くのリレーションシップを使用する必要があります –

答えて

0

を疑問に思ってフォロワーモデルは必要ありません。あなただけのあなたが追加することができ、あなたのUserモデルでMany To Many Relationship

定義することができます:あなたはLaravel Collectionを返し、$user->followingとあなたがものをアクセスできるだけ$user->followersにより、ユーザーフォロワーにアクセスすることができるよりも

public function followers() 
{ 
    return $this->belongsToMany('Shop\User', 'followers', 'user_id ', 'follower_id'); 
} 

public function following() 
{ 
    return $this->belongsToMany('Shop\User', 'followers', 'follower_id', 'user_id'); 
} 

をユーザーはフォローしています。

//Get the count of all followers for $user 
$count = $user->followers()->count(); 

//Get the count of all followed by $user 
$count = $user->following()->count(); 

//Get the username of the first follower 
$follower = $user->followers()->first(); 
echo $follower->username; 

//Loop trough all followers 
foreach ($user->followers as $follower) { 
    echo $follower->username; 
} 

この関係を定義するとサイドノートだけattach()detach()方法

// The $user will be followed by an user with $followerId 
// A record in the `followers` table will be created with 
// user_id = $user->id and follower_id = $followerId 
$user->followers()->attach($followerId); 

// The $user will stop be followed by an user with $followerId 
$user->followers()->detach($followerId); 

を使ってフォロワーを削除/あなたが救うことができます:followers()メソッドを呼び出すと、呼び出しの間に違いがあり followersプロパティ。最初はBelongsToMany関係を返し、あなたはそれのすべての雄弁クエリビルダメソッドを呼び出すことができます以降では、上記Collection

/** @var Illuminate\Support\Collection */ 
$user->followers; 

/** @var Illuminate\Database\Eloquent\Relations\BelongsToMany */ 
$user->followers(); 
0

あなたが関係を実装する方法の例で返されます。フォロワーテーブルを通じて、多くのユーザーに多くのユーザーがいます。フォロワーモデルは必要ありません。すでにユーザーモデルを持っているからです。いくつかは、分析し、機能強化した後、あなたのコードは動作しますが、私は非常に代わり、このinuserモデルのようなものを作るためにあなたをお勧めします:$user->followers

public function followers() { return $this->belongsToMany('App\User','followers','user_id','follower_id'); } public function following_users() { return $this->belongsToMany('App\User','followers','follower_id','user_id'); }

あなたが信者にアクセスすることができるよりも(これは雄弁なコレクションを返し、 laravel docs collection api)とある特定のものを好きなようにすることができます$user->followers[0]

私はあなたの答えrigthを得ることを望みます。