【问题标题】:Search users that follows user搜索关注用户的用户
【发布时间】:2018-02-17 16:41:36
【问题描述】:

我想通过

后面的 ($_POST['search'] '"name" and "lastname"') 搜索用户

"用户 ID = 1"

我得到了表用户,在用户中我得到了

“姓名”和“姓氏”

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

“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;

}

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    关系会是这样的:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多