doutang7383 2018-05-23 13:25
浏览 73

Laravel查询以获取来自同一国家和城市的用户

I am trying to get users from users table. I have another table user_details which stores custom profile fields. The user_details table has four columns id,user,user_register_field,value.

id    user   user_register_field  value

1     1      gender               Male
2     1      city                 somecity
3     1      country              somecountry
4     2      gender               Female
5     2      city                 Null
6     2      country              somecountry
7     3      gender               Male

Users.php

public function Udetails()
{
    return $this->hasMany('App\UserDetails','user');
} 

Suppose user with user id 1 logged in & I want to show him the another users from same Country & City from which the user 1 is from.

  • 写回答

2条回答 默认 最新

  • doujishao8793 2018-05-23 13:38
    关注

    just do something like this:

    User::whereHas('Udetails',function($q){
        $q->where('user_register_field','country')
            ->where('value',your_logged_users_country);
    })->whereHas('Udetails',function($q){
        $q->where('user_register_field','city')
            ->where('value',your_logged_users_city);
    })->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端