搜索关注用户的用户

时间:2018-02-17 16:41:36

标签: laravel eloquent

我想按照

之后的($ _POST ['search']'“name”和“lastname”')搜索用户
  

“user id = 1”

我得到了用户,我得到的用户

  

“name”和“lastname”

我得到了表追随者,我得到了粉丝

  

“follower_id”和“user_id”......

我知道要做到这一点我必须使用相关的模型,但我不知道如何......

//$user_id = 1 (in followers.user_id)
//$_POST['search'] (in users.name and users.lastname)

public function u_followers_search($user_id) {

$followers = ...

return $followers;

}

1 个答案:

答案 0 :(得分:0)

关系'会是这样的:

class User extends model 
{

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

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

和查询

User::whereHas(['follows'=>function($q) use($id){
  $->where('id',$id);
})->where('name',$request->name)->where('lastname',$request->lastname)->get();