2014-02-05 5 views

답변

1

사용자가 둘 이상의 팔로워를 가질 수 있고 사용자가 두 명 이상의 사용자를 추적 할 수 있다고 가정합니다. 같은 모델과 다 대다 관계가 필요합니다.

은 여기에 있습니다 :

다음
<?php 

class User extends Eloquent { 

     ... 

     public function followers() 
     { 
      return $this->belongsToMany('User', 'follows', 'user_id', 'follow_id'); 
     } 

     public function follows() 
     { 
      return $this->belongsToMany('User', 'follows', 'follow_id', 'user_id'); 
     } 

} 

당신이 사용자

$user = User::find(1); 

echo $user->follows()->first()->name; 
echo $user->followers()->first()->name; 

// or 

foreach ($user->followers as $follower) 
     echo $follower->name . '<br>'; 

및 응답에 대한 등

+0

hi.thanks을 볼 수있는 방법입니다. 및 게시와 관련된 내용 내가 그 사람의 게시물을 검색하고 싶다면? –

+0

같은 방법으로 계속되는 메소드를 체인 할 수 있습니다. $ user-> follows() -> first() -> posts() -> first() -> title; 또는 $ user-> follows() -> first() -> $ post로 게시) echo $ post-> title; – diegofelix

+0

나는 .. .. 주저하다 .. –